CakePHP Model display all data null - cakephp

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.

Related

Unsupported operand types on CakePHP

I have a this
**Fatal_Error:
Error: Unsupported operand types
File: C:\wamp\www\newsletter\lib\Cake\View\Helper\FlashHelper.php
Line: 90**
every time I clicked the submit button. here's my code..
**AddsController.php //Controller**
<?php
class AddsController extends AppController {
public $helpers = array('Html','Form','Session');
public $components=array('Session');
public function index() {
if($this->request->is('post'))
{
$this->Add->create();
$this->Add->save($this->request->data);
$this->Session->setFlash('Success');
return $this->redirect(array('action'=>'index'));
}
}
}
?>
**Add.php //Model/**
<?php
App::uses('AppModel' , 'Model');
class Add extends AppModel
{
public $name = "Add";
public $useTable = 'request';
public $primaryket = 'id';
public $useDbConfig = 'default';
}
?>
**index.ctp //View/Adds/index.ctp**
<?php
echo $this->Form->create('add');
echo $this->Form->input('email');
echo $this->Form->submit('submit');
echo $this->Form->end();
?>
dbname: exercise; table: request;
goal: all inputted data must be in the db.
Thank you in advance!
Use FlashHelper to set yor flash messages, not Session->flash
https://book.cakephp.org/2.0/en/core-libraries/helpers/flash.html
// In your Controller
public $helpers = array('Html','Form','Session','Flash');
$this->Flash->set('The user has been saved.', array(
'element' => 'success'
));

Model Association Not Correct

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

Sending Email For cakephp 1.3

in controller :
<?php
App::uses('CakeEmail', 'Network/Email');
class MessagesController extends AppController
{
public $uses = array();
public function send()
{
if (!empty($this->request->data) )
{
$email = new CakeEmail();
$email->from(array('jerold#ballo.com.ph' => 'Jerold Ballo'));
$email->to($this->Email->data['to']);
$email->subject($this->Email->data['subject']);
if ($email->send($this->Email->data['message'])) {
$this->Session->setFlash(__('Email From me'), 'default', array('class' => 'success'));
}
}
}
}
?>
and i got this
Fatal error: Call to undefined method App::uses() in C:\xampp\htdocs\reservation\controllers\messages_controller.php on line 3
Please Help me....
Remove App::uses('CakeEmail', 'Network/Email');
Try
class MessagesController extends AppController
{
public $components = array('Email');
...
You can now use $this->Email the way you have it in the code

cakePHP: User Association with Country and State dropdown

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');

CakePHP Relationship Errors

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.

Resources