Kohana Model Saved Twice - php

I just installed a fresh copy of Kohana 3.2, built my database, wrote my first model, and tried testing it. Everything works fine except the model's "save" method is being executed twice--I end up with two new entries in the database instead of one. The problem only occurs when I use the "find" code shown below.
Why would the model's save get executed twice, once as expected and once because of the find?
Here's the code:
class Controller_Welcome extends Controller {
public function action_index()
{
$rating = ORM::factory('rating');
$rating->user_id = 1;
$rating->userlevel_id = 3;
$rating->category_id = 1;
$rating->page_id = 1;
$rating->rating = 4;
$rating->comments = 'This one is a real killer';
$rating->ratingstatus_id = 1;
$rating->save();
$found = ORM::factory('rating')
->where('id', '=', 1)
->find();
$this->response->body($found->comments); // Test to check for found data
}
} // End Welcome
Thanks in advance!

There are two issues that were causing my problem:
I didn't have a favicon.ico on my server. Many browsers request one, and all URLs that aren't actual files or directories get redirected to the index page. Every time I loaded the page, the browser would request a missing favicon and get redirected to my index page--two requests. After looking at my logs, this page was what tipped me off: http://forum.kohanaframework.org/discussion/7447/error-kohana_request_exception/p1
After I added a favicon, I still saw the double request behavior occasionally. It turns out it was a behavior of Google Chrome--Chrome prefetches pages, so each time I changed the content, Chrome would prefetch and cache the page (adding a request).
After adding a favicon and when using a browser besides Chrome, everything behaves as expected.

$rating = ORM::factory('rating');
This line represents nothing.
If you want to create new record you should use create() instead save().
$rating = new Model_Rating;
$rating->user_id = 1;
$rating->userlevel_id = 3;
$rating->category_id = 1;
$rating->page_id = 1;
$rating->rating = 4;
$rating->comments = 'This one is a real killer';
$rating->ratingstatus_id = 1;
$rating->create();
If you want to load single rating object with given id:
$found = ORM::factory('rating', 1);

Related

Get page number of pdf form field using php

I am currently using SetaPDF to get the form fields located in a document and saving those form field names in a DB. However, I'm trying to get the page number of those form fields. I can't find anything in the Seta documentation that will help with this. Is there another PDF library I can use to accomplish this?
$document = SetaPDF_Core_Document::loadByFilename($file);
$formFiller = new SetaPDF_FormFiller($document);
$fields = $formFiller->getFields();
foreach ($fields->getNames() as $fieldName) {
$field = $fields->get($fieldName);
$is_read_only = 0;
if ($field->isReadOnly()) {
$is_read_only = 1;
}
$is_text = 1;
$field_name = DB::Scrub($fieldName);
$base_field_name = $field->getOriginalQualifiedName();
if (strpos($base_field_name,"#") !== false) {
$arr_field = explode("#", $base_field_name);
$base_field_name = $arr_field[0];
}
if (strpos($base_field_name,"*") !== false) {
$is_text = 0;
$base_field_name = str_replace("*","",$base_field_name);
}
$sql = "INSERT INTO [cust].[PDF_Fields] (file_name,field_name,base_field_name,is_read_only,is_text)
VALUES ('$new_file','$field_name','$base_field_name',$is_read_only,$is_text)";
DB::Query($sql);
}
As Ryan already wrote form fields are not directly related to a page but their representations as Widget Annotations are. This is done by adding the reference to the individual Widget Annotations in the /Annots array of the page. Sadly it is optional to have this the other way round (from the annotation to the page).
You can get the page and its number through some low level methods of the SetaPDF-Core component: First you need an instance of the Widget Annotation of a form field. This can be done with the getAnnotation() method of a form field instance.
Then you can use this instance to search the page via the getPageByAnnotation() method of the main pages instance. To get only the page number you can pass this result to the getPageNumberByPageObject() then.
The written above in code could look like:
$pages = $document->getCatalog()->getPages();
$annotation = $field->getAnnotation();
// Method name is a bit vague and accepts an annotation instance starting with revision > 1371 only
$page = $pages->getPageByAnnotation($annotation->getIndirectObject($document));
// $page = $pages->getPageByAnnotation($annotation); // works with revision > 1371
$pageNumber = $pages->getPageNumberByPageObject($page);

Get pagination results in Active Collab API

I have just discovered you can get pagination results through the api by passing in the page parameter like so:
$projects = $client->get('projects/147/time-records?page=3')->getJson();
Is there a way of knowing how many time records a project has so I know how many times I need to paginate?
Alternatively, how would I go about retrieving several pages worth of data - i'm struggling with the code!
I have created an issue on Github - will await a response.
For now, I do the following:
// Get all the projects
// Set the page number
$page = 1;
// Create an empty array
$project_records = array();
// Get the first page of results
$project_records_results = $client->get('projects?page=' . $page)->getJson();
// Merge the results with base array
$project_records = array_merge($project_records, $project_records_results);
// Get the next page of results,
// if it returns something merge with the base array and continue
while ($project_records_results = $client->get('projects?page=' . ++$page)->getJson()) {
$project_records = array_merge($project_records, $project_records_results);
}
Sure. All paginated results will include following headers:
X-Angie-PaginationCurrentPage - indicates current page
X-Angie-PaginationItemsPerPage - indicates number of items per page
X-Angie-PaginationTotalItems - indicates number of items in the entire data set.
When you get header values, simple:
$total_pages = ceil($total_items_header_value / $items_per_page_header_value);
will give you number of pages that are in the collection.
Alternative: You can iterate through pages (by starting with page GET parameter set to 1, and incrementing it) until you get an empty result (page with no records). Page that returns no records is the last page.
Please note, that the headers are now all lowercase (v1)!
So the answer above should be corrected.
To get them call:
$headers = $client->get($path)->getHeaders();
Working code example from /api/v1/:
$paginationCurrentPage = isset($headers['x-angie-paginationcurrentpage'][0]) ? $headers['x-angie-paginationcurrentpage'][0] : NULL;
$paginationItemsPerPage = isset($headers['x-angie-paginationitemsperpage'][0]) ? $headers['x-angie-paginationitemsperpage'][0] : NULL;
$paginationTotalItems = isset($headers['x-angie-paginationtotalitems'][0]) ? $headers['x-angie-paginationtotalitems'][0] : NULL;

Silverstripe write Page to Live, stage or Draft? How it works

I have some difficulty to update a page by selecting is version... To Create, I do this :
$PageCalendrierEvenement = new PageCalendrierEvenement();
$PageCalendrierEvenement->Title = $this->request->postVar('Titre');
$PageCalendrierEvenement->MenuTitle = $this->request->postVar('Titre');
$PageCalendrierEvenement->URLSegment = Utils::remplacerEspaces(Utils::remplacerAccents($PageCalendrierEvenement->Titre));
$PageCalendrierEvenement->publish('Stage');
$PageCalendrierEvenement->doRestoreToStage();
That work's very well. But how can we update it values by ID? Is it possible witout using DB:Query?
$evens = Versioned::get_by_stage('PageCalendrierEvenement', 'Stage')->byID($evenID);
$evens->Title = $this->request->postVar(Titre);
$evens->Publish('Stage');
For subclasses of SiteTree you can just do:
$evens = Versioned::get_by_stage('PageCalendrierEvenement', 'Stage')->byID($evenID);
$evens->Title = $this->request->postVar(Titre);
//do whatever you want...
$evens->doPublish(); //writes to Stage and Live and does other stuff for SiteTree
For simple DataObjects you need to call:
//changed a bit...
$evens->write();
$evens->publish("Stage", "Live");
See source...

Pagination current page incorrect when 'use_page_numbers'

I'm using CI Pagination library on my website and now with $config['use_page_numbers'] set to TRUE the current page is always the first. Everything works fine, except this.
Other settings:
$config['uri_segment'] = 2;
$config['prefix'] = "p";
$route['mypage/p(:num)'] = "citate/index/$1";
This is the function that calculate the current page (the output is correct). When I'm on first page returns 1, when on third page returns 3 and so on:
function getPagCurr($offset, $limit){
if( ! $offset) return $page = 1;
else return ($offset/$limit)+1;
}
... though, it's not working.
I've try to set up manually, just for testing, the value of $config['cur_page'] = 2 (so this means that the second link should be considered as active) but no change at all.
CI version is latest.
LE: SOLUTION
It seems that the prefix is the problem here. With my actual configuration the link will be like this www.site.com/mypage/p2, which is not working.
The working link would be www.site.com/mypage/p/2/ with the uri_segment = 3 and route mypage/p/(:num).
However, I really want to have the first link structure so here's my solution (not a good one because you have to modify some system library code):
Pagination.php (start line 166):
// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
$this->cur_page = $base_page;
}
..changed to:
// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
$current_page = $CI->uri->segment($this->uri_segment); //get pNUM
$current_page = substr($current_page, 1); //remove prefix
$this->cur_page = $current_page; //set current page
}
... and now it works!
If anybody have a better solution please tell!
Thanks.
Yes you are right it will not work because your segment got a p(p2)
To do this you must have to modify the core but i will say dont modify the core just extend the pagination class and modify the code with following:
Add a new class variable
var $use_rsegment = FALSE;
Then modify the create_links() around line 157
//add
$_uri_segment = 'segment';
if($this->use_rsegment){
$_uri_segment = 'rsegment';
}
//modify
if ($CI->uri->$_uri_segment($this->uri_segment) != $base_page)
{
$this->cur_page = $CI->uri->$_uri_segment($this->uri_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
The uri rsegment is the new routed segment, now set the pagination config like this
$config['use_rsegment'] = TRUE;
$this->pagination->initialize($config);
So you can use both option when ever you need. When you have route set rsegment true

Drupal hook_views_post_execute not getting called

I'm trying to hook into the hook_views_post_execute event in Drupal 7, my module called foo is located in sites/default/modules/features/foo.
My foo.module file contains a definition for the hook_views_api function, defined like this:
function foo_views_api() {
return array("version" => 3.0);
}
This function gets called, but my implementation of the hook_views_post_execute does not, it's defined (in the same foo.module file) like this:
function foo_views_post_execute(&$view) {
$seen_rows = array();
$newResults = array();
for($i = 0; $i < count($view->result); ++$i) {
if (!in_array($view->result[$i]->nid, $seen_rows)) {
$newResults[] = $view->results[$i];
}
$seen_rows[] = $view->result[$i]->nid;
}
$view->result = $newResults;
}
I've been over the drupal API/hooks documentation, googled and read every blog post I've been able to find. I just can't get it to work. The hook does not get called. I'm assuming I've done something simple wrong since I'm not a drupal developer or PHP developer normally.
The view has probably been cached so it doesn't go through that function.
Go to the top left and clear the cache and you should see the result.

Categories