cakephp paginate: data from different tables - cakephp

i have "PostsController" controller and in this controller i need get all user list.
So i need get data no from "Posts" DB table, bu from "Users".
In function I make this paginate:
$this->paginate = array(
'user' => array(
'limit' => 2,
'order' => array(
'user.id' => 'asc',
),
'table' => 'users'
),
);
$users = $this->paginate('user');
$this->set('users', $users);
But always get empty result, why?
And show this error:
Warning (512): Controller::paginate() - can't find model user in controller PostsController [CORE\cake\libs\controller\controller.php, line 1106]

Model name have first letter capitalized, so try
$this->paginate = array(
'User' => array(
'limit' => 2,
'order' => array(
'User.id' => 'asc',
),
'table' => 'users'
),
);
$users = $this->paginate('User');
$this->set('users', $users);

Related

cant get required fields from containable model cakephp

I have a hasmany relationship between Guardian ans Student. A Guardian hasmany Students. I cant get the required fields from containable object students, instead I get everything from Students but I do get the required fields from Guardian.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
$this->Guardian->Behaviors->load('Containable');
$guardians =$this->Guardian->find('all',array(
'contain'=>array('Student',
array( 'fields'=> array('Student.guardian_id,Student.id,Student.first_name' ))),
'order' => array('guardian_first_name ASC'),
'fields'=> array('Guardian.guardian_first_name,Guardian.guardian_last_name,Guardian.id' ),
'recursive'=> -1
));
array(
(int) 0 => array(
'Guardian' => array(
'guardian_first_name' => '',
'guardian_last_name' => '',
'id' => '166'
),
'Student' => array(
(int) 0 => array(
'id' => '166',
'student_inactive' => true,
'student_enq' => false,
'student_unallocated' => false,
'first_name' => 'Kala',
'last_name' => 'narayanan',
The fields option is not nested correctly, which you maybe would have noticed if you'd format your code properly, something along the lines of this:
$this->Guardian->Behaviors->load('Containable');
$guardians = $this->Guardian->find('all', array(
'contain' => array(
'Student',
array(
'fields'=> array(
'Student.guardian_id,Student.id,Student.first_name'
)
)
),
'order' => array(
'guardian_first_name ASC'
),
'fields' => array(
'Guardian.guardian_first_name,Guardian.guardian_last_name,Guardian.id'
),
'recursive'=> -1
));
The array holding the fields option must be passed as the value for Student key.
// ...
'contain' => array(
'Student' => array(
'fields'=> /* ...*/
)
),
// ...
On a side note, when passing the fields as a comma separated string (which might not be the best idea), it's not necessary to pass them in an array.
array(
'fields'=> array(
guardian_id, id, first_name'
)
)
use fields without model name.

Cakephp pagination using contain produces "not associated" warning

I've got the following models:
Run->hasMany ActualResult
ActualResult belongs to Status
When I view an ActualResult, Cake gets the corresponding Status without straight out of the box.
When I view a Run, I want to paginate the ActualResults. I have managed to get this to almost work with the following code in RunsController::view():
public function view($id = null) {
if (!$this->Run->exists($id)) {
throw new NotFoundException(__('Invalid run'));
}
$this->Run->contain ( 'Pack', 'User', 'Status');
$options = array('conditions' => array('Run.' . $this->Run->primaryKey => $id));
$run = $this->Run->find('first', $options);
$this->set('run', $run);
// Paginate the ActualResults
$this->paginate = array(
'contain' => array('Status'),
'order' => 'ActualResult.format',
'limit'=>5 ,
'conditions' => array('ActualResult.run_id' => $id)
);
$actualResults = $this->Paginator->paginate('ActualResult');
$this->set('actualResults',$actualResults);
}
The problem is that I get a warning:
Warning (512): Model "ActualResult" is not associated with model "Status" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 343
Something v. similar works for me in another model association, and as mentioned view() in ActualResultController works fine, so I am stumped.
Can anyone help?
Here are the model assocations:
In Run.php:
public $hasMany = array(
'ActualResult' => array(
'className' => 'actual_result',
'foreignKey' => 'run_id',
'dependent' => true,
'finderQuery' => 'SELECT ActualResult.*, Txn.name, Status.name FROM actual_results AS ActualResult, txns as Txn, codes AS Status WHERE ActualResult.run_id = {$__cakeID__$} and Txn.id = ActualResult.txn_id and (Status.code = ActualResult.status and Status.code_type = "ARS");'
)
);
In ActualResult.php
public $belongsTo = array(
'Run' => array(
'className' => 'Run',
'foreignKey' => 'run_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Status' => array(
'class`enter code here`Name' => 'Code',
'foreignKey' => 'status',
'conditions' => 'code_type = "ARS"',
'fields' => '',
'order' => ''
)
);

Joining two tables in CakePHP using bindModel method Cakephp

I am working on a cakephp 2.x .. I have two tables in my database both have the userid I am using bindModel.. my query is working fine ... I just have one problem .. I want to add the condition in where clause that
**where userid = $userid**
function getMessages($userid){
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'AND' =>
array(
array('OR' => array(
array('Message.mobileNo = Contact.mobileNo'),
array('Message.mobileNo = Contact.workNo'),
)),
)
),
'type' => 'LEFT',
)
)
), false);
return $this->find('all', array('conditions' => array(),
'fields' => array('Message.mobileNo'
),
'group' => 'Message.mobileNo',
'limit' => 6));
}
I am getting user id in parameter ... so I want to add the condition that get the following result where
message.userid and contact.userid = $userid ...
Just split the conditions in two lines like:
'conditions' => array(
'Contact.user_id' => $userid,
'Message.user_id = Contact.user_id',
[...]
)
But the approach that makes more sense is to leave the bind as is - after all the bind is generally between message users and contact users with the same id - and add the condition for the specific case in your find('all') with 'Message.user_id' => $userid.

Cake PHP - How to access a special Model found by Pagination?

I have the following Code to get a special model related to another model related to actual object: (I want the Entry-model - in the Rubric-model-view)
$this->paginate = array(
'Entrieslocation' => array(
'conditions' => array('Entrieslocation.rubric_id' => $this->Rubric->id, 'Entrieslocation.subrubric_id' => '0'),
'joins' => array(
array(
'alias' => 'Entry',
'table' => 'entries',
'type' => 'INNER',
'conditions' => 'Entry.id = Entrieslocation.entry_id'
)
)
)
);
Then I thought I could get it by something like this:
$this->set('entries', $this->paginate('Entry'));
But the variable is empty, how can I access the Entries inside the Pagination in the view?
The Query generated by the code above is correct, I checked it by entering the query into phpmyadmin. So how can I access the result (only the Entries)?
Thanks for your help :)
First of all you must define $paginate variable below controller class. For example
class UsersController extends AppController {
public $paginate = array();
}
Then in your action follow following patter to get the paginated result.
$this->paginate = array(
'recursive' => -1,
'limit' => 10,
'conditions' => array('Your conditions'),
'fields' => array('Entry.*, Entrieslocations.*'),
'joins' => array(
array(
'table' => 'Entrieslocations',
'alias' => 'Entrieslocation',
'type' => 'inner',
'conditions' => 'Entry.id = Entrieslocation.entry_id'
),
),
'order' => array(
'Entry.id' => 'desc'
)
);
$entries = $this->paginate('Entry');
To pass result set to the view use following:
$this->set('entries', $entries);

How to pass a variable in the variable var $paginate with CakePHP

My pagination worked perfectly like this :
var $paginate = array(
'Article' => array(
'conditions' => array(
'Article.visible' => true),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 11
)
);
But I want to filter my articles with a publication date like this
var $paginate = array(
'Article' => array(
'conditions' => array(
'Article.visible' => true,
'Article.publication_date <= ' => date('Y-m-d H:i:s')),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 11
)
);
But the date() function does not seem to be accepted.
It's not the good syntax.
Does anyone can help me?
Thanks a lot in advance.
If you want to do this, you will have to use $this->paginate = array() either in the action that you are paginating in, or in the __construct() function.
You cannot execute functions like that when the object is instantiated.
This is true for any type of method call at runtime.
You can't use a function in the declaration of an array at the class level. What you could do is to use a beforeFilter() callback in your controller to initialize the $paginate variable like this:
function beforeFilter()
{
parent :: beforeFilter();
$this->paginate = array(
'Article' => array(
'conditions' => array(
'Article.visible' => true,
'Article.publication_date <=' => date('Y-m-d H:i:s')),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 11
)
);
}

Resources