I'm getting data by CakePHP findbyList
Array
(
[92] => 5
[93] => 5
[98] => 5
)
CODE
$get = array(
'fields' => array('Upload.type'),
'joins' => array(
array(
'alias' => 't',
'table' => 'temp_files',
'type' => 'LEFT',
'conditions' => array('Upload.id = t.file_id')
),
array(
'alias' => 'o',
'table' => 'orders',
'type' => 'LEFT',
'conditions' => array('o.batch_id = t.batch_id')
)
),
'conditions' => array('OR'=>array('o.user_id '=>$user['id'],'t.user_id '=>$user['id'])),
'group' => 'Upload.id'
);
$files = $this->find('list',$get);
Is there any way in CakePHP for getting data like:
Array
(
[5] => Array
(
[0] => 92
[1] => 93
[2] => 98
)
)
Yes, there is two cakephp ways to retrive results like yours:
Find threaded:
https://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-threaded
and use Hash utility to extract, combine , etc your results:
https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html
Without knowing how your database looks like I would probably use $this->find('all',$get) instead of 'list'
List will always put the ID of your model as a key, so it will never work as you need.
It would have been much better to understand this situation if db structure would have been mentioned.
Anyways what I would suggest is to to use $this->find('all',$get) instead of list and you can play around your final result array by managing the way you need your final output array.
EX:- if you get the list of the user name with userID then you are using CakePHP collection in CakePHP query.
CakePHP collection here :CakePHP Collection
$userList = $this->find()
->where(['status' => Configure::read('Status.active')])
->order(['first_name' => 'ASC'])
->combine('id','name')
->toArray();
in this result in userID is a key and name as a value display.
Related
Suppose I have the following models. Users, Posts, and Comments. Users has many Posts. Posts have many Comments. If I try the following in the Posts controller:
$post = $this->Post->find('first', array('contain' => array('Comment')));
I get an array that looks like this:
array(
'Post' => array(
'postfield1',
'postfield2',
'etc'
),
'Comment' => array(
[0] => array('firstcommentfield1', 'etcfields'),
[1] => array('secondcommentfield1', 'etcfields'),
)
)
But if I run the following from the Users controller,
$user = $this->User->find('first', array('contain' => array('Post' => array('Comment'))));
and then look at the first post for a user $user['Post'][0] I get the following
array(
'Post' => array(
'postfield1',
'postfield2',
'etc'
'Comment' => array(
[0] => array('firstcommentfield1', 'etcfields'),
[1] => array('secondcommentfield1', 'etcfields'),
)
),
)
This makes it difficult to write functions that act on posts without doing formatting manually first.
Anyone have a workaround?
I need to be able to write information to an AWS dynamoDB. However not every write includes the same information, but I would like to use the same code. I can make it work by making multiple requests, but I was wondering if there a way I can refactor this so it can be done with one request?
// This code makes the initial write using what is always posted.
$newRecord = $client->putItem(array(
'TableName' => 'ximoRepV3',
'Item' => array(
'rep_num' => array('S' => $rep_num),
'fc_key' => array('BOOL' => false),
...
)
));
// Then I have a series of if's for writes that are not always present.
if (!empty($salt)){
$saltUpdate = $client->updateItem ( array (
'TableName' => 'ximoRepV3',
'Key' => array (
'rep_num' => array (
'S' => $rep_num
)
),
'ExpressionAttributeValues' => array (
':salt' => array('S' => $salt)
),
'UpdateExpression' => 'SET salt = :salt'
));
}
Right, so my data returns in the following way,
(int) 0 => array(
'MODEL-XX' => array(
//DATA HERE
'xxs_id' => '11',
'aas_id' => '44',
'vvs_id' => '2'
),
'xxs' => array(
'id' => '11',
'customername' => 'XX name here'
),
'aas' => array(
'id' => '44',
'clientname' => 'aa name here',
'xxs_id' => '11'
),
'vvs' => array(
'id' => '2',
'start' => '1405296000',
'end' => '1405814400',
'users_id' => '1'
)
This works fine, but I want to know how to link my users table to this model. So the details of each user for my VV model would become apart of the data. My MODEL-XX does not have any links with my users table so the place I need to call in the users details are held with my VV model.
I have been looking into this but have not been able to find a simple easy method for doing this?
I was thinking that this would be doable with my model, so I opened my my XX model and added the following within my '$belongsTo' section,
'Users' => array(
'className' => 'Users',
'foreignKey' => 'vvs.users_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
So is there a easy method for linking data like this?
Please give me time, if I have not explained myself right or not enough data, please tell me and let me fix or explain better.
Thanks,
Either set your recusive higher:
$this->MODEL-XX->recursive = 1; //or 2
Or and this should be your prefered way to go, start using the containable behaviour:
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
In your appModel:
public $actsAs = array('Containable');
Then try this find:
$this->MODEL-XX->recursive = -1;
$data = $this-MODEL-XX>find(
'all', array(
'conditions' => $conditions,
'contain' => array('xxs', 'aas', 'vvs', 'user')
)
);
I might be 'vvs.user' but I forgot what is used for deeper models
Taking a sub panel in sugarcrm and making it into two, one that displays entries depending if a checkbox is checked, the other non checked entries.
I have a module called mod_loaninvestor, and its sub panel is whole_subpanel_mod_loaninvestor_contacts
Now I know vardefs is where you create the duplicate and rename it like this
<?php
//WARNING: The contents of this file are auto-generated
// created: 2014-01-24 13:12:28
$layout_defs["Contacts"]["subpanel_setup"]['mod_loaninvestor_contacts'] = array (
'order' => 100,
'module' => 'mod_LoanInvestor',
'subpanel_name' => 'default',
'sort_order' => 'asc',
'sort_by' => 'id',
// 'where' => "(mod_LoanInvestor.active_investment == "1")",
'title_key' => 'LBL_MOD_LOANINVESTOR_CONTACTS_FROM_MOD_LOANINVESTOR_TITLE',
'get_subpanel_data' => 'mod_loaninvestor_contacts',
'top_buttons' =>
array (
/*
0 =>
array (
'widget_class' => 'SubPanelTopButtonQuickCreate',
),
1 =>
array (
'widget_class' => 'SubPanelTopSelectButton',
'mode' => 'MultiSelect',
),
*/
),
);
// created: 2014-01-24 13:12:28
$layout_defs["Contacts"]["subpanel_setup"]['mod_loaninvestor_contacts1'] = array (
'order' => 100,
'module' => 'mod_LoanInvestor',
'subpanel_name' => 'default',
'sort_order' => 'asc',
'sort_by' => 'id',
'title_key' => 'LBL_MOD_LOANINVESTOR_CONTACTS_FROM_MOD_LOANINVESTOR_TITLE',
'get_subpanel_data' => 'mod_loaninvestor_contacts',
'top_buttons' =>
array (
/*
0 =>
array (
'widget_class' => 'SubPanelTopButtonQuickCreate',
),
1 =>
array (
'widget_class' => 'SubPanelTopSelectButton',
'mode' => 'MultiSelect',
),
*/
),
);
?>
How would I make it so the first one would only show entries with the following checked
and the other display only entries with that field unchecked?
Thanks for your help!
You're halfway there. On top of defining new layoutdefs, you need to create a new subpanel definition.
Dig into mod_LoanInvestor and the metadata/subpanels/default.php and copy it to default2.php or whatever you'd like to call it. You'll want to use a WHERE clause to separate the two.
https://gist.github.com/matthewpoer/8871568
Here's a gist where I'm doing this with transactions records to seperate scheduled (future) transactions from past (posted payments).
I am using Cakephp 2.2.4 and I need to retrive a list of Lead that belongs to the user (id = 106).
The result of the query is:
array(
(int) 0 => array(
'Lead' => array(
'id' => '6',
'user_id' => '106',
'date' => '2012-12-31 22:15:23',
'ip' => '127.0.0.1',
'service_id' => '1',
'location' => 'Rome',
'message' => 'Message Message',
'telephone' => null,
'active' => null
),
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Service' => array(
'id' => '1',
'name' => 'Primo servizio'
),
'Estimate' => array(
(int) 0 => array(
'id' => '1',
'lead_id' => '6',
'user_id' => '106'
)
)
)
)
It looks good but I need to count the Estimates (Estimate array), I would like to retrive the number of the estimates, and not the array with all the fields (of estimates table).
How can i do it?
I need :
Lead array as it shown
User array as it shown
Service array as it shown
Estimate (only the total number of the estimates... in this case 1)
The find is very simple:
$options = array('conditions' => array('User.id' => 106));
debug($this->Lead->find('all', $options));
Try something like this, not 100% sure it'll work but worth a go if not I'd advise trawling the cakephp docs for retrieving your data:
$options = array(
'fields' => array('Lead.*', 'User.*', 'Service.*', 'count(Estimate.id)'),
'conditions' => array('User.id' => 106)
);
Without diving too far into the internals of the Cake ORM, assuming you don't need to do this immediately at query time, couldn't you just get the count of the estimate array programmatically after the fetch?
http://php.net/manual/en/function.count.php
$leads = $this->Lead->find('all',$options);
foreach($leads as $lead){
//get the number of estimates for every lead result
$lead_num = count($lead['Estimate']);
}
Alternatively, you could manually write a join query for this one fetch and execute it using the Cake Model class's query method. Without knowing the specifics of your table schema and model relations its hard to give specifics about how to structure the query, but this shouldn't be too difficult by just look at your table spec and extracting a sql COUNT statement for every Estimate with given id.