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
Related
I'm just learning cakePHP. I have a CATEGORIES table and a POSTS table. A post have one category. So there is a category_id foreign key in POSTS table.
In my Model, I have Category.php with this code.
<?php
App::uses('AppModel', 'Model');
class Category extends AppModel {
// A hasMany association will allow us to fetch a category’s posts when we fetch a Category record.
public $primaryKey = 'category_id';
public $hasOne = 'Post';
}
?>
In my Post.php I have something like this.
class Post extends AppModel {
public $hasMany = array('Photo');
public $primaryKey = 'post_id';
public $belongsTo = array('User'); // 'Category',
public $relatedImages;
public $belongsTo = 'Category';
Now in my PostController.php I have
$allCategories = $this->Category->find('list');
$this->set('allCategories', $allCategories);
The objective is to retrieve all the category from the categories table and show it in my form.
However I encountered the error of Call to a member function find() on a non-object
I'm not sure what is happening here. How can I retrieve this?
many thanks!
Use
$this->Post->Category->find('list');
As you have to goto Category table Thru posts as u r in posts controller Now currently.
In your post model try to declare the Category as below
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id'
)
);
Is there a limit to how deep a select all will go for a many-to-may relationship? I have a design like this:
Table Survey has many Table Survey Sections. Table Survey Sections have many Questions. Table Questions have many Table Possible Responses.
If I do this for the Survey Table:
$this->loadModel('Survey');
$this->Survey->recursive = 2;
$getSurvey = $this->Survey->find('all');
It will only go as far as the Questions table but bring back the Possible Responses Table. But, if I query the Survey Sections table, it brings everything back. Is this an issue with my code or a limit to how deep a search will go?
Here is my Models:
<?php
App::uses('AppModel', 'Model');
class Survey extends AppModel {
public $validate = array(
);
public $belongsTo = array(
);
public $hasMany = array(
'surveySection' => array(
'className' => 'SurveySection',
'foreignKey' => 'surveys_id',
),
);
}
<?php
App::uses('AppModel', 'Model');
class SurveySection extends AppModel {
public $validate = array(
);
public $belongsTo = array(
);
public $hasMany = array(
'Question' => array(
'className' => 'Question',
'foreignKey' => 'survey_section_id',
),
);
}
<?php
App::uses('AppModel', 'Model');
class Question extends AppModel {
public $validate = array(
);
public $belongsTo = array(
);
public $hasMany = array(
'PossibleResponse' => array(
'className' => 'PossibleResponse',
'foreignKey' => 'questions_id',
),
);
}
<?php
App::uses('AppModel', 'Model');
class PossibleResponse extends AppModel {
public $validate = array(
);
public $belongsTo = array(
);
}
You should use $this->Survey->recursive = 3;.
As I said in the comment, the example at http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive is a bit misleading because it only show an example for a specific case. In fact you can go as far as you want when fetching data.
In Cakephp,
I have used belongsTo in AppModel
class AppModel extends Model {
public $actsAs = array('Containable');
public $belongsTo = array(
'ModifiedBy' => array(
'className' => 'User',
'foreignKey' => 'common_modified_by_user_id'
)
);
}
Now I also want to apply belongs to in UserModel
class User extends AppModel {
public $belongsTo = array(
'School' => array(
'className' => 'User',
'foreignKey' => 'school_id'
)
);
}
But when i do so...it over rides the belongsTo of App model and on School data comes in the fetched array....
Please Help...
No automatic merging of associations
Other than the $actsAs and $findMethods variables, association configurations are not being merged automatically, you'll have to do that on your own.
It should be rather simple to solve, using Object::_mergeVars() in your User models constructor should do the trick:
public function __construct($id = false, $table = null, $ds = null) {
$parent = get_parent_class($this);
$this->_mergeVars(array('belongsTo'), $parent);
parent::__construct($id, $table, $ds);
}
ps. you might want to have a look at behaviors, they might be the better option for your needs.
I just created 2 models UsersModel and UserpicsModel and try to get both records but it is returning Error: Call to a member function find() on a non-object.
//UsersModel.php
class Users extends AppModel
{
public $hasMany = array(
'Userpics' => array(
'className' => 'Userpics'
)
);
}
//UserpicsMdoel.php
class Userpics extends AppModel
{
public $belongsTo = array(
'Users' => array(
'className' => 'Users',
'foreignKey' => 'uid'
)
);
}
//RecipesController.php
class RecipesController extends AppController {
public $uses =array('Users','Userpics');
public function view() {
$users = $this->Users->Userpics->find('all');
print('<pre>');
print_r($users);
print('<pre>');
exit;
}
}
first: you are not following cake conventions: models should be singular and not plural.
But the actual problem here is that the model files names are wrong: if you still want to use your conventions then the name for the Users model should be Users.php and not UsersModel.php
the same for Userpics
read this useful answer about how to debug this kind of error.
But if you decide to use the cake naming conventions (and I strongly suggest you to do so) consider doing the following:
//User.php (table users)
class User extends AppModel
{
....
}
//UserPic.php (table user_pics)
class UserPic extends AppModel
{
....
}
//RecipesController.php
class RecipesController extends AppController {
public $uses =array('User','UserPic');
public function view() {
$users = $this->User->UserPic->find('all');
print('<pre>');
print_r($users);
print('<pre>');
exit;
}
}
//UsersModel.php
class Users extends AppModel
{
public $hasMany = array(
'Userpics' => array(
'className' => 'Userpics',
'foreignKey' => 'uid'
)
);
}
Hi
I'm trying to to do pagination from the KindsController but with data from the LinkModel
Tables :
Kinds links
id id
name title
order url
kind_id
order
model:
class Kind extends AppModel{
var $name = 'Kind';
var $hasMany = 'Link';
}
controller:
class KindsController extends AppController{
var $name = 'Kinds';
var $paginate = array(
'Kind'=> array(
'limit' => 12,
'order'=> array('Kind.order'=>'ASC')
)
);
}
desired result (view) ;
Kind.name ( numbers of links )
links.name
........
..........
Kind.name ( numbers of links )
links.name
........
..........
i'm using the kinds.order and links.order to control how the data is displayed in the view
from an admin area.
i need to retrive data from the kinds table ordered by kinds.order ASC and the number of links, and the links data ordered by links.order
thanks
You can define order in the relation like this:
class Kind extends AppModel{
var $hasMany = array(
'Link' => array(
'className' => 'Link',
'order' => 'Link.name'
),
);
}