Associations: HasMany with a BelogsTo inside it - cakephp

Hi I am trying out CakePHP 2.5 and created the blog from the tutorial.
I'm adding comments to blog posts, each comment has a user, but I can't seem to pull back the user information.
in my user model
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
in my comments model
public $belongsTo = [
'User' => [
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
],
'Posts' => [
'className' => 'Posts',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
]
];
and in the postmodel, post being whenre I want to display the comment and the username
public $belongsTo = [
'User' => [
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
]
];
public $hasMany = [
'Comment' => [
'className' => 'Comment',
'order' => 'Comment.created DESC',
'foreignKey'=> 'post_id'
]
];
viewing a post brings back the following comment information
'Comment' =>
array (size=1)
0 =>
array (size=6)
'id' => string '1' (length=1)
'content' => string 'This is a comment' (length=17)
'created' => string '2015-10-23 15:59:55' (length=19)
'modified' => string '2015-10-23 15:59:55' (length=19)
'user_id' => string '1' (length=1)
'post_id' => string '2' (length=1)
But this doesn't include the user information as I expected.
My question is how do I pull the user information for each comment within a post

Sorry, I don't have points for commenting. Could you post your find() method? Also, do you have recursion turned on, or is it off (turned to -1) and you are using contain to grab associated models?

Related

Missing Datasource Configuration when run cakephp project in live

Showing error when run project in live server.error as been below
Missing Datasource Configuration Error:
The datasource configuration
default was not found in database.php.
my database.php file
class DATABASE_CONFIG {
public $default;
function __construct(){
$this->default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'xxxxxx',
'database' => 'demo',
'prefix' => '',
);
}
}
please help me to resolve that issue. Thanks in advance
Please use below code :
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'your_db_name',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
change in first db configuration only do not change in $test configuration. Leave as it is.
I got a default equal to this, see if it helps
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'tutorial',
'prefix' => '',
//'encoding' => 'utf8',
);

Database configuration Issue CakePHP

My database.php :
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/MySql',
'persistent' => false,
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'cake_database',
'schema' => '',
'prefix' => '',
'encoding' => 'utf8'
);
public $test = array(
'datasource' => 'Database/MySql',
'persistent' => true,
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'cake_database',
'prefix' => '',
//'encoding' => 'utf8',
);
}
Shows this notice : CakePHP is NOT able to connect to the database.Database connection "Mysql" is missing, or could not be created.
Please help...........
public $default = array( 'datasource' => 'Database/MySql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'cake_database', 'schema' => '', 'prefix' => '', 'encoding' => 'utf8' );
Mysql default login is root and password empty,
If you have change it, you must enter the user and the password to access database..
It's just because cakephp failed connect to mysql..

Can I count number of return value format json in cakephp?

Method in Controller
public function getLike() {
$this->Feed->UserLikeFeed->recursive = 1;
$this->Paginator->settings = array(
'contain' => array('UserLikeFeed','UserCommentFeed','UserShareFeed')
);
$like_comment_shares = $this->Paginator->paginate();
$this->set(array(
'like_comment_shares' => $like_comment_shares,
'_serialize' => array('like_comment_shares')
));
}
Model
public $hasMany = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'feed_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'UserCommentFeed' => array(
'className' => 'UserCommentFeed',
'foreignKey' => 'feed_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'UserLikeFeed' => array(
'className' => 'UserLikeFeed',
'foreignKey' => 'feed_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'UserShareFeed' => array(
'className' => 'UserShareFeed',
'foreignKey' => 'feed_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
When I access the method in controller,it returns value like the image below. I want to get number of data. Ex: UserLikeFeed, it returns two value. I want to get 2. What should I do now? Thank you for helping.
Result
{
"like_comment_shares": [
{
"Feed": {
"id": "72",
"admin_id": "2",
"title": "New term",
"description": "This is a new term"
},
"UserCommentFeed": [
{
"user_id": "1",
"feed_id": "72",
"created": null,
"description": "Going home"
},
{
"user_id": "1",
"feed_id": "72",
"created": null,
"description": "Going School"
}
],
"UserLikeFeed": [
{
"user_id": "1",
"feed_id": "72"
},
{
"user_id": "2",
"feed_id": "72"
}
],
"UserShareFeed": [
]
},
Because you are paginating your data you will only get the values for that specific page based on your pagination limit, rather than thinking about counting the json results, simply count the results from the array before you serialize it to JSON, basically you can do something like:
$userlikefeed_count = count($like_comment_shares['UserLikeFeed']);
Which will return the number of items in that array, you can then send this data to the controller as well:
$this->set(array(
'like_comment_shares' => $like_comment_shares,
'userlikefeed_count' => $userlikefeed_count
'_serialize' => array('like_comment_shares', 'userlikefeed_count')
));

CakePHP bindTranslation() returns only records for current locale

I want to retrieve all records from the original table (category) with their translations from translation table (category_i18ns). I want all the records from the table category, regardless of my current locale settings.
Currently when I use bindTranslation(), it returns the all the records from the translation table, but only for the records (from table categories) that match the current locale.
Here is an example:
Records in table category_i18ns
id,created,modified,locale,model,foreign_key,field,content
1,"2014-08-15 17:08:26","2014-08-15 17:08:26",eng,Category,1,name,Entertainment
2,"2014-08-15 17:08:26","2014-08-15 17:08:26",eng,Category,1,url_name,entertainment
3,"2014-08-15 17:09:47","2014-08-15 17:09:47",eng,Category,2,name,Location
4,"2014-08-15 17:09:47","2014-08-15 17:09:47",eng,Category,2,url_name,location
5,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,2,name,Locatie
6,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,2,url_name,locatie
7,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,3,name,Vervoer
8,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,3,url_name,vervoer
9,"2014-08-15 17:12:11","2014-08-15 17:12:11",deu,Category,1,name,Unterhaltung
10,"2014-08-15 17:12:11","2014-08-15 17:12:11",deu,Category,1,url_name,unterhaltung
11,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,1,name,Vermaak
12,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,1,url_name,vermaak
13,"2014-08-15 17:12:11","2014-08-15 17:12:11",deu,Category,2,name,Lage
14,"2014-08-15 17:12:11","2014-08-15 17:12:11",deu,Category,2,url_name,lage
15,"2014-08-15 17:12:11","2014-08-15 17:12:11",eng,Category,3,name,Transport
16,"2014-08-15 17:12:11","2014-08-15 17:12:11",eng,Category,3,url_name,transport
17,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,4,name,"Eten & Drinken"
18,"2014-08-15 17:12:11","2014-08-15 17:12:11",nld,Category,4,url_name,eten-drinken
Records in table categories
id,created,modified,total_services
1,"2014-08-15 17:08:26","2014-08-15 17:08:26",0
2,"2014-08-15 17:09:47","2014-08-15 17:09:47",0
3,"2014-08-15 17:12:11","2014-08-15 17:12:11",0
4,"2014-08-15 17:12:11","2014-08-15 17:12:11",0
CategoriesController.php
$query2 = array(
'recursive' => 1
);
$this->Category->bindTranslation(array(
'name' => 'name_i18n',
'url_name' => 'url_name_i18n'
));
$categories = $this->Category->find(
'all',
$query2
);
debug($categories);
Result of $categories
array(
(int) 0 => array(
'Category' => array(
'id' => '1',
'created' => '2014-08-15 17:08:26',
'modified' => '2014-08-15 17:08:26',
'total_services' => '0',
'locale' => 'eng',
'name' => 'Entertainment',
'url_name' => 'entertainment'
),
'Service' => array(),
'name_i18n' => array(
(int) 0 => array(
'id' => '1',
'created' => '2014-08-15 17:08:26',
'modified' => '2014-08-15 17:08:26',
'locale' => 'eng',
'model' => 'Category',
'foreign_key' => '1',
'field' => 'name',
'content' => 'Entertainment'
),
(int) 1 => array(
'id' => '9',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'deu',
'model' => 'Category',
'foreign_key' => '1',
'field' => 'name',
'content' => 'Unterhaltung'
),
(int) 2 => array(
'id' => '11',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'nld',
'model' => 'Category',
'foreign_key' => '1',
'field' => 'name',
'content' => 'Vermaak'
)
),
'url_name_i18n' => array(
(int) 0 => array(
'id' => '2',
'created' => '2014-08-15 17:08:26',
'modified' => '2014-08-15 17:08:26',
'locale' => 'eng',
'model' => 'Category',
'foreign_key' => '1',
'field' => 'url_name',
'content' => 'entertainment'
),
(int) 1 => array(
'id' => '10',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'deu',
'model' => 'Category',
'foreign_key' => '1',
'field' => 'url_name',
'content' => 'unterhaltung'
),
(int) 2 => array(
'id' => '12',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'nld',
'model' => 'Category',
'foreign_key' => '1',
'field' => 'url_name',
'content' => 'vermaak'
)
)
),
(int) 1 => array(
'Category' => array(
'id' => '2',
'created' => '2014-08-15 17:09:47',
'modified' => '2014-08-15 17:09:47',
'total_services' => '0',
'locale' => 'eng',
'name' => 'Location',
'url_name' => 'location'
),
'Service' => array(),
'name_i18n' => array(
(int) 0 => array(
'id' => '3',
'created' => '2014-08-15 17:09:47',
'modified' => '2014-08-15 17:09:47',
'locale' => 'eng',
'model' => 'Category',
'foreign_key' => '2',
'field' => 'name',
'content' => 'Location'
),
(int) 1 => array(
'id' => '5',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'nld',
'model' => 'Category',
'foreign_key' => '2',
'field' => 'name',
'content' => 'Locatie'
),
(int) 2 => array(
'id' => '13',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'deu',
'model' => 'Category',
'foreign_key' => '2',
'field' => 'name',
'content' => 'Lage'
)
),
'url_name_i18n' => array(
(int) 0 => array(
'id' => '4',
'created' => '2014-08-15 17:09:47',
'modified' => '2014-08-15 17:09:47',
'locale' => 'eng',
'model' => 'Category',
'foreign_key' => '2',
'field' => 'url_name',
'content' => 'location'
),
(int) 1 => array(
'id' => '6',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'nld',
'model' => 'Category',
'foreign_key' => '2',
'field' => 'url_name',
'content' => 'locatie'
),
(int) 2 => array(
'id' => '14',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'deu',
'model' => 'Category',
'foreign_key' => '2',
'field' => 'url_name',
'content' => 'lage'
)
)
),
(int) 2 => array(
'Category' => array(
'id' => '3',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'total_services' => '0',
'locale' => 'eng',
'name' => 'Transport',
'url_name' => 'transport'
),
'Service' => array(),
'name_i18n' => array(
(int) 0 => array(
'id' => '7',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'nld',
'model' => 'Category',
'foreign_key' => '3',
'field' => 'name',
'content' => 'Vervoer'
),
(int) 1 => array(
'id' => '15',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'eng',
'model' => 'Category',
'foreign_key' => '3',
'field' => 'name',
'content' => 'Transport'
)
),
'url_name_i18n' => array(
(int) 0 => array(
'id' => '8',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'nld',
'model' => 'Category',
'foreign_key' => '3',
'field' => 'url_name',
'content' => 'vervoer'
),
(int) 1 => array(
'id' => '16',
'created' => '2014-08-15 17:12:11',
'modified' => '2014-08-15 17:12:11',
'locale' => 'eng',
'model' => 'Category',
'foreign_key' => '3',
'field' => 'url_name',
'content' => 'transport'
)
)
)
)
As you can see, records with id 17 and 18 from table category_18ns and 4 from table categories are not included in the result ($categories). I want that this also are included, regardless of the current locale.
For now I have a quick and dirty solution for my case. I'm retrieving data by my own sql statement. But I now this is not a save solution. So if somebody has a better (more secure) solution, I would like to know that.
My current quick and dirty solution:
Category.php (model)
/**
* Returns all categories with all translated fields, independent of locale setting.
*/
public function getAllTranslations() {
$result = null;
$queryResults = $this->query('SELECT categories.*, category_i18ns.* FROM categories INNER JOIN category_i18ns ON category_i18ns.foreign_key = categories.id;');
foreach($queryResults as $queryResult) {
$locale = $queryResult['category_i18ns']['locale'];
$field = $queryResult['category_i18ns']['field'];
$content = $queryResult['category_i18ns']['content'];
$categoryId = $queryResult['categories']['id'];
if(!isset($result[$categoryId])) {
$result[$categoryId] = $queryResult['categories'];
$result[$categoryId][$field][$locale] = $content;
} else {
$result[$categoryId][$field][$locale] = $content;
}
}
return $result;
}

how i can fetch user data in a manyToMany association

I have a problem with CakePHP. I tested many ways, but can't find a solution for it.
I have these tables:
users
id
name
country_code
countries
code
title
skills
code
title
roles
id
title
events
id
title
country_code
events_users
event_id
user_id
role_id
skill_code
events hasAndBelongsToMany users and events_users is my join table, and when I read data, it gives me something like this:
array(
(int) 0 => array(
'Event' => array(
'id' => '1',
'title' => '40th WorldSkills Competitions',
'year' => '2011',
'country_code' => 'LV',
'created' => '2013-02-08 00:00:00'
),
'User' => array(
(int) 0 => array(
'password' => '*****',
'id' => '14',
'name' => 'test',
'family' => 'testian',
'username' => 'test#mail.me',
'country_code' => 'BR',
'telphone' => '465465',
'avatar' => 'avatar_51299c1da268f20110912043238_rodekamp_04.jpg',
'address' => 'AA',
'admin' => '0',
'created' => '2013-02-24 05:50:37',
'modified' => '2013-02-24 05:50:37',
'EventsUser' => array(
'id' => '1',
'user_id' => '14',
'event_id' => '1',
'role_id' => '1',
'skill_code' => '17',
'manage' => '100'
)
),
(int) 1 => array(
'password' => '*****',
'id' => '5',
'name' => 'John',
'family' => 'Smith',
'username' => 'john#me.com',
'country_code' => 'IE',
'telphone' => '147',
'avatar' => '20120504124040_franziska_peter_ok.jpg',
'address' => 'No 55',
'admin' => '0',
'created' => '2013-02-10 15:24:13',
'modified' => '2013-02-10 15:28:50',
'EventsUser' => array(
'id' => '2',
'user_id' => '5',
'event_id' => '1',
'role_id' => '2',
'skill_code' => '17',
'manage' => '0'
)
),
(int) 2 => array(
'password' => '*****',
'id' => '4',
'name' => 'hadi',
'family' => 'mm',
'username' => 'design.zerzem#gmail.com',
'country_code' => 'AE',
'telphone' => '415456',
'avatar' => '',
'address' => 'sadjfklsaf asd f',
'admin' => '0',
'created' => '2013-02-10 09:01:28',
'modified' => '2013-02-24 06:43:42',
'EventsUser' => array(
'id' => '3',
'user_id' => '4',
'event_id' => '1',
'role_id' => '4',
'skill_code' => '17',
'manage' => '0'
)
)
)
It's ok, but with events_users I want get data about skill and role that are in events_users (skill_code and role_id). How should I create models to give me all this data?
I think reading about "hasMany Through" should answer your question.
Basically, you make a model for your join table, then you can associate it like any other model.
When you go to retrieve your data, use CakePHP's Containable behavior (if you haven't used it before, be prepared to get addicted to it).

Resources