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';
Related
I am few months old with CakePHP. This is first time I am trying CakePhp Association. I assume I am following almost all instruction but still my model doesn't seem to work.
This is simple 'User' and 'Profile' Model. Table Structure: User:
- id (Primary Key)
- name
Profile:
- id (primary key)
- role
- user_id (Reference Key)
Models: User:
class UserModel extends AppModel{
var $name = 'User';
public $hasOne = array(
'Profile'
);
}
Profile:
class ProfileModel extends AppModel{
var $name = 'Profile';
public $belongsTo = array('User'); }
Controller: Users:
lass UsersController extends AppController{
var $name = 'Users';
public $scaffold;
function index(){
$user = $this->User->find('all'); //HERE I expect to get User and Profiles data
pr($user);
}
}
Profiles:
class ProfilesController extends AppController{
var $name = 'Profiles';
public $scaffold;
function index(){
$profile = $this->Profile->find('all');
pr($profile);
}
}
If I run users: /localhost/test_php_apps/users/ I get:
Array (
[0] => Array
(
[User] => Array
(
[id] => 1
[name] => rohini
)
)
)
I am wondering why 'Profile' data is not shown. I have manually added records in tables.
Further if I try in UsersController: $user = $this->User->Profile->find('all'); I get the following error:
Call to a member function find() on a non-object
My guess is something is wrong with setting up Associations. But not sure what is messing things up.
I know this is very basic question, but even after reading 10 to 15 related cases I don't seem to find the answer.
Any help will be much appreciated.
Do me a favor, change
class UserModel extends AppModel
to
class User extends AppModel
and
class ProfileModel extends AppModel
to
class Profile extends AppModel
and see if that helps when doing $user = $this->User->Profile->find('all'); in the UsersController.
Also, is
public $actsAs = array('Containable');
not
public $actAs = 'Containable';
not actAs. See if that helps any. If not, it would be helpful to know your cake version.
See here for containable and this for naming model conventions.
If you bake this things is made your life easier.
Models: User:
class User extends AppModel{
var $name = 'User';
public $hasMany = array(
'Profile'=>array(
'className' => 'Profile',
'foreignKey' => 'user_id',)
);
}
Profile:
class Profile extends AppModel{
var $name = 'Profile';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
)
);
}
Controller: Users:
class UsersController extends AppController{
var $name = 'User';
public function index(){
$users = $this->User->find('all');
var_dump($users);
}
}
Controller Profile:
class ProfilesController extends AppController{
var $name = 'Profile';
public function index(){
$user_id = 2;
$users = $this->Profile->find('all', array('conditions'=>array('Profile.user_id'=>$user_id)));
var_dump($users);
}
}
add the following lines in your usermodel
public $useTable = 'YOUR USERTABLENAME';
and change
public $hasOne = array(
'Profile'
);
to public $hasOne = 'Profile';
in your profilmodel add the same.
public $useTable = 'YOUR PROFILETABLENAME';
and change
public $belongsTo = array('User');
to
public $belongsTo = array('User' => array('className' => 'User',
'foreignKey' => 'user_id'));
in your userscontontroller add this
public $uses = array('User','Profile');
normally it should work now when you try the query
$u = $this->User->find('all',array('contain' => array('Profile'));
or
$u = $this->User->find('all',array('recursive' => 2));
but it also should work if you write only:
$u $this->User->find('all');
regards
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');
}
To the extension of my other question here
I seeing the following error while doing an ajax call for populating the states dropdown -
2011-12-15 01:15:51 Debug: Populating state for country id #99
2011-12-15 01:15:51 Debug: Notice (8): Undefined property: User::$State in [C:\xampp\htdocs\dearmemoir\app\controllers\users_controller.php, line 236]
Here is my code from the controller -
function get_states($country_id = null){
CakeLog::write('debug', 'Populating state for country id #' . $country_id ) ;
$this->layout = 'ajax';
$this->set('states',$this->User->State->find('list',array('conditions'=>array('State.country_id' => $country_id))));
}
I see that ajax call is being made and I am able to display the selected value in the controller.
And here are Models -
state.php
<?php
class State extends AppModel {
var $displayField = 'state_name';
var $name = 'State';
var $hasMany = array('User');
var $hasOne = array ('Country');
}?>
country.php
<?php
class Country extends AppModel {
var $displayField = 'country_name';
var $name = 'Country';
var $hasMany = array('User','State');
}?>
user.php
<?php
class User extends AppModel {
var $name = 'User';
var $belongsTo = array('Country');
------
}
I am unable to figure out what might possible be wrong!
urg...i figured out-
My user model is missing the association with State
var $belongsTo = array('Country');
should be
var $belongsTo = array('Country','State');
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