I've been working this issue this whole weekend, and I can't seem to find a solution.
I have three models:
User Class:
class User extends AppModel {
var $hasMany = array('Like','Event');
}
Like Class:
class Like extends AppModel {
var $belongsTo = array ('Event','User');
}
Event Class:
class Event extends AppModel {
var $belongsTo = 'User';
var $hasMany = 'Like';
}
This is my find statement:
$this->set('likes', $this->Like->find('all', array(
'contain'=>array(
'User','Event')));
My like table is:
id(int 10)
user_id(int 10)
event_id(int 10)
My user table has:
id(int 10)
plus all regular user related column
My event table has:
id(int 10)
user_id (int 10)
Plus all other event related columns
NOW THE PROBLEM:
[0]
[Like]
id:77
user_id:30
event_id:130
[Event]
id(null)
user_id(null)
title(null)
date(null)
etc......
[User]
id:30
email:********#gmail.com
password:**********************
created:2013-07-07 23:45:13
modified:2013-07-07 23:45:13
etc.........
This is my query:
SELECT `Like`.`id`, `Like`.`user_id`, `Like`.`event_id`, `Event`.`id`, `Event`.`user_id`, `Event`.`title`, `Event`.`date`, `Event`.`price_advance`, `Event`.`price_door`, `Event`.`price_vip`, `Event`.`created`, `Event`.`bcg_img`, `Event`.`event_info`, `Event`.`event`, `Event`.`phone1`, `Event`.`phone2`, `Event`.`promoter`, `User`.`id`, `User`.`email`, `User`.`password`, `User`.`created`, `User`.`modified`, `User`.`isAdmin`, `User`.`verified`, `User`.`full_name` FROM `quadb`.`likes` AS `Like` LEFT JOIN `quadb`.`eventss` AS `Event` ON (`Like`.`event_id` = `Event`.`id`) LEFT JOIN `quadb`.`users` AS `User` ON (`Like`.`user_id` = `User`.`id`) WHERE 1 = 1
I don't know why my event array is coming back null. Any help will be greatly appreciated.
EDIT
After changing the find code to add an order clause, it magically works, I don't know exactly why:
$this->set('likes', $this->Like->find('all', array(
'order' => array('Like.created' => 'DESC'),
'contain'=>array('Quad'))));
class Event extends AppModel {
var $belongsTo = array('User');
var $hasMany = array('Like');
}
Related
I've set up two models: city, and country. Below is how I've defined them:
class City extends AppModel { // for "cities" table
public $hasOne = 'Country';
}
class Country extends AppModel { // for "countries" table
public $hasMany = array(
'City' => array(
'className' => 'City'
)
);
}
and in my controller:
public function getCities() {
$this->loadModel('City');
$cities = $this->City->find('all');
}
but it's giving me this error:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Country.city_id' in 'on clause'
SQL Query: SELECT `City`.`id`, `City`.`country_id`, `City`.`name`, `City`.`latitude`, `City`.`longitude`, `City`.`time_zone`, `City`.`dma_id`, `City`.`code`, `City`.`created`, `City`.`modified`, `Country`.`id`, `Country`.`country_id`, `Country`.`name`, `Country`.`code`, `Country`.`created`, `Country`.`modified` FROM `rtynbiz_ls`.`cities` AS `City` LEFT JOIN `rtynbiz_ls`.`countries` AS `Country` ON (`Country`.`city_id` = `City`.`id`) WHERE 1 = 1
Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp
I can't understand why it's trying to do a join with Country table. I only want to fetch cities. How do I stop this from happening? And, why is it trying to make an association using Country.city_id (which doesn't exists) Also, have I named my classes and tables correctly? Thanks
Following lines of code are making relationship so JOIN is present there. To remove the JOIN from the query just replace :
class City extends AppModel { // for "cities" table
public $hasOne = 'Country';
}
class Country extends AppModel { // for "countries" table
public $hasMany = array(
'City' => array(
'className' => 'City'
)
);
}
With:
class City extends AppModel { }// for "cities" table
class Country extends AppModel {} // for "countries" table
You can make relationships in the tables easily by following this
By default, CakePHP will automatically try to pull additional model's data. To stop that from being the default, in your AppModel, set this variable:
public $recursive = -1;
I would also suggest adding Containable behavior, so your app model looks like this:
<?php
class AppModel extends Model {
public $actsAs = array('Containable');
public $recursive = -1;
}
Read more about "recursive" and "Containable Behavior" at the CakePHP book.
If you don't want to do join when you want to retrieve your data make it recursive -1
public function get_cities() {
$this->loadModel('City');
$this->City->recursive=-1;
$cities = $this->City->find('all');
//or
$cities = $this->City->find('all', array('recursive'=>-1));
}
any way this would be your model:
class Country extends AppModel {
public $hasMany = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'country_id',
'dependent' => false,
),
);
}
class City extends AppModel {
public $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
)
);
}
make sure you don't have messy code on these 2 models
I am having three tables category, subcategory and subsubcategory.
Now category table contain fields as id,name
subcategory table contain fields as id, category_id and name
and subsubcategory table contain fields as id, subcategory_id and name
now subsubcategory view/index showing columns as name,subcategory_name
but i want to display category_name also in my subsubcategory index page
I have used containable behaviour to associate two tables as category and subsubcategory
Here is my code:--
subsubcategoriescontroller:
$contain =array(
'Subcategory'=> array(
'Category' =>array(
'fields' => array('id', 'name')
)
));
$this->Subsubcategory->find('all',array('contain' => $contain));
appmodel:
public $actsAs = array("Containable");
still my subsubcategory page not showing expected results.
what will i do?
Have you got the correct relationships set up in your models?
class Category extends AppModel {
public $hasMany = array('Subcategory');
}
class Subcategory extends AppModel {
public $belongsTo = array('Category');
public $hasMany = array('Subsubcategory');
}
class Subsubcategory extends AppModel {
public $belongsTo = array('Subcategory');
}
I got two tables: users and firms:
class Firm extends AppModel {
var $name = 'Firm';
var $hasAndBelongsToMany = 'User';
}
and:
class User extends AppModel {
var $name = 'User';
var $hasMany ='Post';
var $hasAndBelongsToMany = 'Firm';
}
I use scaffold var to display all save/view/edit etc methods. I override the add method like this:
function add(){
if (!empty($this->data)) {
$this->User->create();
$this->User->save($this->data);
$this->redirect(array('action'=>'index'), null, true);
}
$firms = $this->User->Firm->find('list');
$this->set('firms', $firms);
}
Everything works great, but when i use users/add, I got dropdown with id: "1","2",etc.
I would like to display Firm name, not id. How to do that?
You need to set the 'displayField' property in your models, this is what Cake uses in various built in functions, including find('list');` This defaults to 'name' or 'title' field in your database, but I'm guessing you don't have those fields in the firms database.
Change it with this in your Firms model:
var $displayField = 'firm_name';
I'm getting this error in my cakephp application
Warning (512): SQL Error: 1054: Unknown column 'Category.post_id' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
I'm assuming that this error was caused by how I set up relationships in the models since the error states that it was looking for 'Category.post_id', a field that doesn't exist.
Here's the category model code:
class Category extends AppModel {
var $name = 'Category';
var $belongsTo = 'Post';
}
and post model code:
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = 'User';
var $hasMany = 'Category';
}
it shows up on several methods, but here's my post index action:
function index() {
$this->set('posts', $this->Post->find('all'));
}
Any idea how I can fix this?
Create another table called posts_categories with columns id, post_id, category_id.
then your Post Model
class Post extends AppModel {
public $name = 'Post';
public $hasAndBelongsToMany = array('Category');
}
then you Category Model
class Category extends AppModel {
public $name = 'Category';
public $hasAndBelongsToMany = array('Post');
}
You should establish your primary key id in your model, if it`s not the same as autogenerated is (for model post it is post_id). So if your table primary key name is 'id', your post model should be
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = 'User';
var $hasMany = 'Category';
var $primaryKey = 'id';
}
I did not have a post_id field in my database. Added that column and my problem was solved.
I'm trying to build an invitation system with CakePHP. I have Invitation model which hasMany People, and Person hasOne and belongsTo Invitation. I believe the relationships are working, since my Invitation index view can properly access and display its associated People through the Invitation model. But heres what the relationship rules look like:
class Invitation extends AppModel {
var $name = 'Invitation';
var $hasMany = array(
'People' => array(
'className' => 'Person',
'foreignKey' => 'invitation_id',
'dependent'=> true
)
);
...
}
class Person extends AppModel {
var $name = 'Person';
var $hasOne = 'Invitation';
var $belongsTo = 'Invitation';
...
}
UPDATED MODEL RELATIONSHIPS:
class Invitation extends AppModel {
var $name = 'Invitation';
var $hasMany = array('Person');
...
}
class Person extends AppModel {
var $name = 'Person';
var $belongsTo = array('Invitation');
...
}
In my Invitation add function, however, I am having trouble saving the data for new People. I want to simultaneously add one invitation and two people.
Heres my add function:
function add() {
$this->autoRender = false;
if($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
if(!empty($this->data)) {
$this->Invitation->create();
if($this->Invitation->saveAll($this->data)) {
//debug($this->data);
$this->Session->setFlash('Saved');
$this->redirect('invitations/add_invitations');
} else {
echo('Didnt save<br />');
}
}
}
}
Here is the output of my debug($this->data):
Array
(
[Invitation] => Array
(
[code] => 001
[password] => 85c8a3735499bf91d25e5960ab4ed9deeb0b457e
[type] => 1
[num_people] => 2
)
[Person] => Array
(
[0] => Array
(
[fname] => Jane
[lname] => Doe
)
[1] => Array
(
[fname] => John
[lname] => Doe
)
)
)
I don't get any errors. the Invitation data is saved properly, but the people are not.
UPDATE: I finally got this working with the above updated model relationships. Obviously the hasOne & belongsTo rules in the Person model were conflicting. After that I made a mistake (an embarrassing one) that was preventing saving to related the related Person model:
In the Invitation model, I hade $hasMany = array('People'); when it should have been $hasMany = array('Person').
I also had to change the configuration of my database to use InnoDB as the default, so that was certainly a necessary fix.
var $hasOne = 'Invitation';
var $belongsTo = 'Invitation';
These relations to the same model should have different Aliases otherwise Cake will get confused, as I did trying to unpick it. See http://book.cakephp.org/view/1046/Multiple-relations-to-the-same-model