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');
Related
I can not display the data.
My model (app/Model/Product.php)
class Product extends AppModel {
public $name = 'Product';
}
My controller (app/Controller/ProductsController.php):
class ProductsController extends AppController {
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public $name = 'Products';
public function ver($id = null) {
$this->Product->id_producto = $id;
debug($this->Product->id_producto); //Show id with value Ex. 12,34,...
$this->set('products', $this->Product->read());
}
}
My view (app/View/ver.ctp):
<?php debug($product['Product']['nombre']); ?> // Show 'null'
<h2><?php echo $product['Product']['nombre']?></h2></td></tr>
<strong>Descripción:</strong> <?php echo $product['Product']['descripcion']?> <br/>
<small>Fecha de registro: <?php echo $product['Product']['fecha_reg']?></small> <br/>
<small>Última modificación: <?php echo $product['Product']['Fecha_mod']?></small>
You are not using the default primary key: id. Define your primary key in your model:
class Product extends AppModel
{
public $primaryKey = 'id_producto';
}
... and assing requesting product id to id in your controller. id will match to primary key id_producto in your database table:
public function ver($id = null)
{
$this->Product->id = $id;
debug($this->Product->id); //Show id with value Ex. 12,34,...;
$this->set('product', $this->Product->read());
}
Some notes:
In your controller you assign the results to plural 'products':
$this->set('products', $this->Product->read());
But in your view you use the variable in singular $product:
Try to use <?php debug($products['Product']['nombre']); ?> instead.
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 am developing a news portal with cakephp 2.4.
I have 2 tables( categories and articles)
now I want to show articles from a given category by this link: http://mydomain/category/2
my category model:
class Category extends AppModel {
var $name = 'Category';
var $hasMany = array('Article');
}
my article model:
class Article extends AppModel {
var $name = 'Article';
var $belongsTo = Array('Category');
Article controller:
class ArticlesController extends AppController {
var $name = 'Articles';
var $scaffold;
function lists()
{
$this->Article->recursive = 1;
$myCat = $this->Category->find('all', array('order' => 'Category.id ASC'));
return $this->Article->lists($myCat);
}
}
my category controller:
class CategoriesController extends AppController {
var $name = 'Categories';
var $scaffold;
function index()
{
$this->Category->recursive = 1;
$this->set('showCat', $this->Category->find('all'));
}
}
Now I am confused how to show the articles list by http://mydomain/category/2
please help me
Now I am confused how to show the articles list by /mydomain/category/2
In your categories controller
function index($categoryId = null) {
$this->Category->recursive = 1;
if ($categoryId) {
$this->set('articles', $this->Category->Article->find('all', array('conditions' => array('Article.category_id' => $categoryId)));
}
}
Now, in you categories/index.ctp you will have $articles available.
<?php
if (isset($articles)) {
//etc etc
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.