Let's say I have an index action where I want to get a list of projects:
$this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
It works well and returns the following array:
Array ( [0] => Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) ) )
How do I modify the find function, so it returns the array in the following format:
Array ( [projects] => Array ( [0] => Array ( [id] => 2 [title] => test project ) [1] => Array ( [id] => 1 [title] => first project ) ) )
Thank you!
You could use the Set::combine() utility method to do this. I've used it for similar means as so:
public function groupByMenu() {
return Set::combine (
$this->find (
'all',
array (
'conditions' => array ( 'NavItem.active' => 1 ),
'order' => 'NavMenuItem.display_order'
)
),
'{n}.NavItem.title',
'{n}',
'{n}.NavMenu.id'
);
}
The code above takes a set of navigation items and reorganizes them so that they're grouped by the menu(s) they are displayed within.
It's not really clear if it's the fact that the result is under 'Project' rather than 'projects', but if you don't like that it's under [0] I believe you could use PHPs array_shift:
$result = $this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
$result = array_shift($result);
The result will be:
Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) )
Related
In cakephp 2 I want to group table after descending order
always returned the first record of table
but I want returned last record by ordering
my data, code and result in here:
My data in table:
Code in controller:
$books = $this->Book->find('all', array(
'order' => array('age' => 'DESC'),
'group' => 'name'
));
print_r($books);
Result after the code above:
Array
(
[0] => Array
(
[Book] => Array
(
[id] => 1
[name] => ramin
[family] => zardoshti
[age] => 32
[city] => semnan
)
)
[1] => Array
(
[Book] => Array
(
[id] => 2
[name] => esmaeil
[family] => seydi
[age] => 30
[city] => semnan
)
)
)
But I want this result:
Array
(
[0] => Array
(
[Book] => Array
(
[id] => 1
[name] => ramin
[family] => zardoshti
[age] => 42
[city] => tehran
)
)
[1] => Array
(
[Book] => Array
(
[id] => 2
[name] => esmaeil
[family] => seydi
[age] => 30
[city] => semnan
)
)
)
Add a virtual field on the fly to your Book model
$this->Book->virtualFields = array(
'max_age' => sprintf('max(%s.age)', $this->Book->alias)
);
$books = $this->Book->find('all', array(
'order' => array('age' => 'DESC'),
'group' => 'name'
));
print_r($books);
The virtual field will help to get the rows with the maximum age value.
Your SQL query will look like :
SELECT
`Book`.`id`, `Book`.`name`, `Book`.`family`, `Book`.`age`,
`Book`.`city`, (max(`Book`.`age`)) AS `Book__max_age`
FROM
`test`.`books` AS `Book`
WHERE
1 = 1
GROUP BY
name
ORDER BY
`age` DESC
Hi i don't know what is wrong with the threaded of cakephp 2 please help
when i use threaded without condition it will show its children
$category = $this->Category->find('threaded');
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 113
[name] => Core samples
[parent_id] => 11
)
)
)
but when i use this with condition it won't show its parent
$this->Category->find('threaded',array('conditions'=>array('Category.id'=>11)));
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
)
)
)
please help why it is not showing the children when use with condition
I think the problem is because you are asking for all categories with an id of 11. Insted you should ask for all categories that are children of categorie 11.
You could use your left and right columns to get a nested array (keyword Tree behaviour). Try this:
$parentCategorie = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.id' => 11
)
);
$children = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.lft BETWEEN ? AND ?' => array($parentCategorie['Categorie']['lft'], $parentCategorie['Categorie'])['rght']
)
);
Probably the best chance you have to achive what you want is to use the children method of the Tree behavior.
You coul use it as follows
$parent=$this->Category->find('first',
array('conditions'=>array('Category.id'=>11)));
$this->Category->children($parent['Category']['id']);
I have a problem with the Laravel 5 Validation, I post data with the Angular method $http.post. I have allready created a Request file called, CreateProjectRequest.
The posted data looks like this.
Array(
[0] => Array
(
[project_id] =>
[title] => test
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => 'test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
[1] => Array
(
[project_id] =>
[title] => test2
[tasks] => Array
(
[0] => Array
(
[vat_id] => 1
[title] => test
[quantity] =>
[rate] =>
[costs] =>
)
)
)
)
I need to validate title and tasks, how can I reach that?
I tried a few things but the I get each time a error. For example title is required while title was filled in.
// Empty rules array
$rules = [];
foreach ($this->request->all() as $keyGroup => $request)
{
$rules['title'] = 'required';
// Loop throug tasks
foreach ($request['tasks'] as $key => $task)
{
$rules['quantity'] = 'required|numeric';
}
}
return $rules;
I'm trying to convert the updated datetime column to a number. So I did this in my Containable behavior.
$this->paginate = array(
'limit'=>1,
'fields'=>array(
'id',
),
'contain'=>array(
'Image'=>array('fields'=>array(
'id',
'UNIX_TIMESTAMP(Image.updated) AS `rev`',
))
)
);
I get this data from Paginator
Array
(
[0] => Array
(
[Receipt] => Array
(
[id] => 19938
)
[Image] => Array
(
[0] => Array
(
[id] => 18143
[receipt_id] => 19938
[Image] => Array
(
[0] => Array
(
[rev] => 1357872726
)
)
)
[1] => Array
(
[id] => 18144
[receipt_id] => 19938
[Image] => Array
(
[0] => Array
(
[rev] => 1357872728
)
)
)
)
)
)
For some reason Image is nested twice for each record. If I remove the UNIT_TIMESTAMP it gets the data correctly.
$this->paginate = array(
'limit'=>1,
'fields'=>array(
'id',
),
'contain'=>array(
'Image'=>array('fields'=>array(
'id',
'updated',
))
)
);
Output
Array
(
[0] => Array
(
[Receipt] => Array
(
[id] => 19938
)
[Image] => Array
(
[0] => Array
(
[id] => 18143
[updated] => 2013-01-10 21:52:06
[receipt_id] => 19938
)
[1] => Array
(
[id] => 18144
[updated] => 2013-01-10 21:52:08
[receipt_id] => 19938
)
)
)
)
Any idea why Cake is going this when I add an expression to a field?
You can try using virtual fields:
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->virtualFields['name'] = sprintf(
'UNIX_TIMESTAMP(%s.updated) AS %s.rev', $this->alias, $this->alias
);
}
Not sure if my notation is right, give it a try, I haven't tested it.
When I try to create a new entry of Template with the $data array, the models of Template, Group and Product are saved correctly. But the nested Calcfield (which is a hasMany of Group) is not saved. :( Group and Product are hasMany of Template.
Is this possible at all?
$this->Template->create();
$this->Template->saveAll($data, array('validate' => false));
// $data looks like this:
Array
(
[Template] => Array
(
[title] =>
[shorttitle] => Wie auch immer
[place] => Hannover
[size] =>
)
[Group] => Array
(
[0] => Array
(
[title] => Hosting
[order] =>
[Calcfield] => Array
(
[0] => Array
(
[title] => Hosting
[value] => 0
[description] =>
)
[1] => Array
(
[title] => Strom
[value] => 0
[description] =>
)
)
)
)
[Product] => Array
(
[0] => Array
(
[share] => 10
[businessunit] => Marketing
)
[1] => Array
(
[share] => 30
[businessunit] => intl. CRM
)
)
)
No, recursive saves are not possible, as far as I know. You would need to stick those in a separate array and then save them after the initial save.
Since CakePHP 2.1 it's possible to save deeply-nested models using the deep parameter in saveAll (since CakePHP 2.1)
ref. http://book.cakephp.org/2.0/en/models/saving-your-data.html?highlight=saveall#model-saveassociated