Retrieve specific table fields in Cakephp - cakephp

i have a table in which i want to get only two columns data .. right now i am using findAll method ... i don't know how can I get the specific two fields data in CakePHP
$recentContacts = $this->Contact->find('all',
array(
'order'=>'Contact.idContacts DESC',
'limit' => 6,
'conditions' => array(
'Contact.User_id' => $id)));
in my contacts table there are two fields one is "name" and other is "number"
which I want to extract ...

You can do this by adding fields attribute.
$recentContacts = $this->Contact->find('all',
array
(
'order'=> array( 'Contact.id' , 'Contacts DESC'),
'limit' => 6,
'fields' => array(
'Contact.name',
'Contact.number'
),
'conditions' => array
(
'Contact.User_id' => $id
)
));

you can use like this way with your same code to add fields
$recentContacts = $this->Contact->find('all',
array(
'order'=>'Contact.idContacts DESC',
'limit' => 6,
'fields' => array(
'Contact.name',
'Contact.number'
),
'conditions' => array(
'Contact.User_id' => $id)));
in previous answer they have changed you id instead of idContacts, you can just copy my code and solve your problem.
let me know if i can help you more.

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 unable to sort on afterfind virtual fields with paginate

I have a query that I am running through paginate. This query contains a model ("PaymentException") that has an afterfind method that tacks on a copy of the last "ExceptionWorkflowLog", and calls it "LastWorkflowLog".
The query being passed to paginate:
$this->paginate = array(
'fields' => array(
'PaymentException.*', 'Procedure.id', 'Procedure.cpt',
'Procedure.expected_amount', 'Procedure.allowed_amount', 'Procedure.difference_amount',
'Claim.id', 'Claim.number', 'Payer.abbr'
),
'limit' => 50,
'joins' => array(
array(
'table' => 'procedures',
'alias' => 'Procedure',
'conditions' => array('Procedure.id = PaymentException.procedure_id')
),
array(
'table' => 'claims',
'alias' => 'Claim',
'conditions' => array('Claim.id = Procedure.claim_id')
),
array(
'table' => 'payers',
'alias' => 'Payer',
'conditions' => array('Payer.id = Procedure.payer_id')
),
array(
'table' => 'groups',
'alias' => 'Groups',
'conditions' => array('Groups.id = Claim.group_id')
)
),
'conditions' => $conditions,
'contain' => array('ExceptionWorkflowLog')
);
The resulting array (from the query that combines both "PaymentException", "ExceptionWorkflowLog", and "LastWorkflowLog") looks like below:
0 =>
'PaymentException' => array(fields and values),
'ExceptionWorkflowLog' => array(of ExceptionWorkflowLogs),
'LastWorkflowLog' => array(fields and values of the last indexed ExceptionWorkflowLog)
1 => ...
ExceptionWorkflowLog is mapped to PaymentException by PaymentException.id. It's a many to one relationship (thus the array of results under the ExceptionWorkflowLog).
I would like to use paginate to sort on the "updated" field on either the last indexed ExceptionWorkflowLog or the LastWorkflowLog.
Is there a way to do this with paginate? Currently, if I set the table heading to point to "LastWorkflowLog.updated", the query returns false because the query doesn't know what "LastWorkflowLog" is.
Since this has a couple hundred views, I figured I'd come back and post what I did. CakePHP's handling of joins is absolutely terrible. I rewrote the query to not use joins, but use contains. That seems to have solved it. I feel dirty.

how can i concat first_name and last_name two different database fields to assign to drop down list in cakephp using 'list' in find statement

$swimmer = $this->Swimmer->find('list', array(
'conditions' => array('Swimmer.group' => $this->data['Swimmer']['group_id']),
'order' => array('Swimmer.first_name ASC'),
'fields' => 'Swimmer.first_name'
));
First of all, setup a virtual field in your Swimmer model as suggested by Ann Pham. eg:
var $virtualFields = array(
'name' => "CONCAT(Swimmer.first_name, ' ', Swimmer.last_name)"
);
Then, fetch the data for your dropdown list like so: (assuming Swimmers controller)
$this->Swimmer->find('list', array('fields' => array('Swimmer.id', 'Swimmer.name')));
You could also try doing this in your SwimmerModel: var $displayName = 'Swimmer.name'; (not 100% sure if this would work). If it does work, you won't need the 'fields' array in the find.
An another approach is to concat the field on the fly in the fields list.
Ex:
$swimmer = $this->Swimmer->find('list',
array(
'conditions' => array(
'Swimmer.group' => $this->data['Swimmer']['group_id']),
'order' => array(
'Swimmer.first_name ASC'
),
'fields' => 'CONCAT(Swimmer.first_name, " ", Swimmer.first_name) AS full_name'
));
http://book.cakephp.org/view/1588/virtualFields

cakephp using paginate and order

HI,
why not work "order" function?
$this->paginate = array(
'limit' => 5,
'order' => array(
'User.name' => 'desc'
),
'fields' => array('Post.id', 'Post.title', 'User.name AS aut_name'),
'joins' => array(
array(
'table' =>'users',
'alias' =>'User',
'type' =>'LEFT',
'conditions' => array(
'Post.user_id = User.user_id'
)
)
)
);
$posts = $this->paginate();
$this->set(compact('posts'));
DB structure:
posts:
id, title,body, created, updated, user_id
users:
user_id, name
Looking quickly your code... Does field user_id exist in User table?
'conditions' => array(
'Post.user_id = User.id'
)
Because you're specifying User.name As aut_name in your fields, you won't be able to order by User.name unless you also have User.name in your fields list. Alternatively use:
'order' => array(
'aut_name' => 'desc'
),
NOTE: This is for the initial query only, to sort by aut_name from the View you'll need to use a Virtual Field in the User model.
Also, as #Min said, are your conditions correct?
Adding a solution on the same problem:
This work for cakePHP 2.3.4
CakePHP Paginate conditions on Join Table

Problem with excluding conditions in complex cakephp model

I'm fairly new to cakephp and I'm having a problem with a complicated find function on my models.
I have a number of Groups each containing a number of Users, and each group can make a number of Orders. Each order consists of a number of OrderAmounts which contain an amount and a user_id (relating to a User).
I have a find which finds an Order, and returns all users in the Group relating to that Order and any OrderAmounts corresponding to that user:
$currentOrder = $this->Order->find('first', array(
'conditions' => array(
'Order.group_id' => $this->Session->read("Auth.User.group_id")
),
'contain' => array(
'Group' => array(
'User' => array(
'OrderAmount' => array(
'OrderAmountType'
)
)
)
)
));
What I now want to do is to return a list of all the Users in the Group relating to the Order above who do not have a corresponding OrderAmount.
So far I have this, but I'm not sure where to put the condition to exclude users with OrderAmounts - if I put the conditions in the contains part it simply removes the OrderAmounts from the model, and if I put them in the top level conditions in the find I get an error.
$currentOrderOutstanding = $this->Order->Group->User->find('all', array(
'conditions' => array(
'Group.id' => $this->Session->read("Auth.User.group_id")
),
'fields' => array('User.id'),
'contain' => array(
'OrderAmount',
'Group'
)
));
Take a look at this example from the tutorial:
$this->User->find('all', array(
'contain'=>array(
'Profile',
'Account' => array(
'AccountSummary'
),
'Post' => array(
'PostAttachment' => array(
'fields' => array('id', 'name'),
'PostAttachmentHistory' => array(
'HistoryNotes' => array(
'fields' => array('id', 'note')
)
)
),
'Tag' => array(
'conditions' => array('Tag.name LIKE' => '%happy%')
)
)
)
));
Notice that all the conditions are inside the 'contain' array. Does it help?

Resources