List posts that a user likes in CakePHP app - php

I have two tables: Posts and Likes
And I'm trying to list the posts that the current logged in user likes. The method looks like the following:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
So basically what I'm trying to do is first query the following (likes) table for all matching rows to the user and then use this array of post ids in my find query to list posts that were in the query.
The listFollowing method in the Follower model looks like:
public function listFollowing($user_id)
{
return $this->find('all', array(
'conditions' => array('Follower.user_id'=>$user_id)
));
}
I'm currently getting an error like: Undefined index: Follower [APP/Controller/PostsController.php, line 94] So gonna presume that the way I'm trying to pass the list of post ids from the following in the find query is incorrect.
Can anyone help? Thanks
Edit: Doing a debug on $following gives:
array(
(int) 0 => array(
'Follower' => array(
'id' => '4',
'user_id' => '6',
'post_id' => '136'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '136',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content',
'slug' => 'Test_content',
'content' => 'Test Content',
'status' => '1'
)
),
(int) 1 => array(
'Follower' => array(
'id' => '5',
'user_id' => '6',
'post_id' => '133'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '134',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content 2',
'slug' => 'Test_content_2',
'content' => 'Test Content 2',
'status' => '1'
)
)
)

Couple of questions:
Did you checked what query is being executed? If it's executed then probably the method is correct.
is $following being populated? what happens if you debug it?
if you are retreiving with 'all' then the result will look like this:
Array(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
)
So you'll never be able to find something at $following['Follower']. Check Cake's documentation for this
Based on you comments, if you need a list of id's try this inside your method:
public function listFollowing($user_id)
{
return $this->find('list', array(
'conditions' => array('Follower.user_id'=>$user_id)
'fields' => array('Follower.user_id'),
'recursive' => 0);
}
The 'list' retrieve mode does exactlye that.

It is hard to guess the error with this limited information but I'm guessing that it's a PHP error related with your $following array.
Can you try putting debug($following) after your listFollowing() function call? So in the end, it should look like this:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
debug($following);
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
You will see what is returned from listFollowing() that is causing an index error. You can further debug it with that information. I can no longer help as there is no database schema, full code, the variables/situation that causes this problem, etc :)

Related

WordPress update option multi-dimensional array

I have wordpress option_name student_data with option value a:7:{s:6:"status";s:1:"2";s:13:"name";s:73:"valuename";s:9:"address";s:32:"valueaddress";s:13:"school";s:32:"valueschool";s:4:"mother";s:270:"valuemother";s:12:"father";s:340:"valuefather";s:13:"total";s:131:"valuetotal";}
Below is my code to update the data, but it doesn't work.
$option_array = array(
'status' => '2',
'name' => 'valuename',
'address' => 'valueaddress',
'school' => 'valueschool',
'mother' => 'new_valuemother',
'father' => 'new_valuefather',
'total' => 'valuetotal'
);
update_option('student_data', $option_array);
Is there any solution? Thanks.

php insert key/value into associative array

I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}

Cakephp: How to get data with specific fields on associated model

I'm using cakephp framework. I have two tables on my DB, courses and modules table. Those tables are related, courses has many modules. Basically I want to get data on courses and on modules but with specific fields. I want to get just the id and the title of the course and the id and the title of the module.
This code:
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title')
));
gives me:
array(
(int) 0 => array(
'Course' => array(
'id' => '1',
'title' => 'course 1'
),
'Module' => array(
(int) 0 => array(
'id' => '1',
'course_id' => '1',
'title' => 'module 1',
'image' => null,
'content' => 'Lorem ipsum',
'voice_over' => null,
'created' => '2014-09-03 14:02:25',
'modified' => '2014-09-03 14:02:28'
),
(int) 1 => array(
'id' => '2',
'course_id' => '1',
'title' => 'module 2',
'image' => null,
'content' => 'Sasdas',
'voice_over' => null,
'created' => null,
'modified' => null
)
)
),
(int) 1 => array(
'Course' => array(
'id' => '2',
'title' => 'course 2'
),
'Module' => array()
)
)
But I wanna select a specific field for module as well so I tried this code:
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title', 'Module.id', 'Module.title')
));
but gives me an error: Column not found: 1054 Unknown column 'Module.id' in 'field list'.
Please help me guys thanks.
Got the answer, To make sgt's answer work, $actsAs property should be added in Course model. Reference : http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
Either you use some behavior like Containable and your find() will look like this :
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title'),
'contain' => array(
'Module' => array(
'fields' => array('Module.id', 'Module.title'),
),
),
));
try this -
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title'),
'contain' => array('Module.id', 'Module.title')
));

Has many save cakePHP

I have a situation where I have hasMany relations between two objects. request data looks like this
array(
'Project' => array(
'name' => 'Projekt Y',
'main_contractor' => 'APOS - Zagreb',
'main_contractor_id' => '188',
'arhitect_id' => '20'
),
'ProjectAssociate' => array(
(int) 0 => array(
'project_role_id' => '1',
'Person' => array(
'input' => 'First name, last name',
'firm_id' => ''
)
),
(int) 1 => array(
'project_role_id' => '1',
'Person' => array(
'input' => '',
'firm_id' => ''
)
)
)
)
The problem is, that I can not save this data because validation does not allow Person.input to be empty. Is there a way to cancel the save proces only for ProjectAssociate.1? I want to successfully save the rest of the data
Try to unbind your ProjectAssociate model
// Let's remove the hasMany...
$this->Project->unbindModel(
array('hasMany' => array('ProjectAssociate'))
);
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

Using Containable behavior to filter results in Cakephp 2.2.4

I'm using containable behavior and the result of my find('all') is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
(int) 0 => array(
'lead_id' => '6',
'Estimate' => array(
(int) 0 => array(
'TOT_count' => '2'
)
)
)
)
)
)
I need to to count how many estimates there are in the lead.
The total (2) is correct, but i see nested 'Estimated' array, why ?
The result i would like to get is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
'TOT_count' => '2'
)
)
)
This is the find:
$options = array(
'contain' => array(
'User',
'Estimate' => array(
'fields' => 'COUNT(*) AS TOT_count'
)
),
'conditions' => array('Lead.id' => 6),
'fields' => 'User.*',
'limit' => 1
);
debug($this->Lead->find('all', $options));
How can i do it?
Thanks!
When you use a "custom" AS statement, in your case TOT_count, Cake will always put this in a result key called 0. You can avoid this by defining TOT_count as a virtualField in your model. That way it will be nested directly under the model name in your resultset.
Secondly, the lead_id is forcedly retrieved, because it is "needed" to make the join with the Lead model. It can not properly retrieve all the data without that piece of information there.

Categories