Unsupported operand types on CakePHP - 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'
));

Related

save() function in cakephp save null values

My problem is that i dont know save() function of cakephp insert null values in posts table !
When I try to add a post: http://i.stack.imgur.com/fhRVn.png
After redirecting the inserted data is null: http://i.stack.imgur.com/YstBb.png
My PostsController.php is:
App::uses('AppController', 'Controller');
class PostsController extends AppController{
public $components = array('Session');
public $helpers = array('Html', 'Form');
public function index() {
$this -> set('posts', $this->Post->find('all'));
}
public function view($id = null){
if(!$id){
throw new NotFoundException(_('il faut choisir un post'));
}
$post=$this->Post->findById($id);
if(!$post){
throw new NotFoundException(_('invalide Post !'));
}
$this->set('post',$post);
} // fin de "view"
public function add(){
if($this->request->is('post'))
{
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
//debug($this->Post->validate);
}//fin d'ajout
} // fin de "appcontroller"
My Post.php model file is:
class Post extends appModel {
//public $name='Post';
public $validate = array(
'title' => array('rule' => 'notEmpty'),
'body' => array('rule' => 'notEmpty')
);
}
My view form post is:
echo $this->Form->create('post');
echo $this->Form->input('title');
echo $this->Form->input('body',array('rows' => '3'));
echo $this->Form->end('Ajouter');
in your view maybe your problem that you Model is Post not post
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body',array('rows' => '3'));
echo $this->Form->end('Ajouter');

CakePHP Model display all data null

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.

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 save() doesn't save data

I don't see anything wrong with my code. But it does not save data:
<?php
class ProductsController extends AppController{
var $name = 'Products';
//var $helpers = array('Form');
//var $scaffold;
function index(){
$this->Product->recursive = 1;
$products = $this->Product->find('all');
$this->set('products',$products);
//pr($products);
}
function add(){
$categories = $this->Product->Category->find('list',array(
'field'=>array('Category.categoryName')
));
$this->set('categories',$categories);
if(!empty($this->data)){
if($this->Product->save($this->data)){
$this->Session->setFlash('Saved');
}
}
}
}
?>
it flashes "Saved" but nothing is being inserted in my table. What could possibly be wrong when it should be functioning properly. :(
Below is my add.ctp model:
<h2>ADD</h2>
<?php echo $this->Form->create('Product',array('action'=>'add')); ?>
<?php
echo $form->input('ProductName');
echo $form->input('categories');
echo $form->end('DONE');
?>
You have to use the create() method before to save
function add(){
$categories = $this->Product->Category->find('list',array(
'field'=>array('Category.categoryName')
));
$this->set('categories',$categories);
if(!empty($this->data)){
$this->Product->create();
if($this->Product->save($this->data)){
$this->Session->setFlash('Saved');
}
}
}

Why do I get these errors:?

My error code is:
Notice: Undefined variable: form in
c:\AppServ\www\applogic\app\views\users\index.ctp on line 1
Fatal error: Call to a member function create() on a non-object in
c:\AppServ\www\applogic\app\views\users\index.ctp on line 1)))
(index.ctp)
<?php echo $form->create(null, array('action' => 'index'));?>
<fieldset>
<legend>Enter Your Name</legend>
<?php echo $form->input('name'); ?>
</fieldset>
<?php echo $form->end('Go');?>
(users_controller.php)
<?php
class UsersController extends AppController {
var $name = 'Users';
var $uses = array();
function index() {
if (!empty($this->data)) {
//data posted
echo $this->data['name'];
$this->autoRender = false;
}
}
}
?>
Did you set the $helpers in app_controller or users_controller? You need to include 'Form' in it.
If you are using 2.0, I think you need to use $this->Html (not $html)

Resources