CakePHP DB field update fail - php

I am using cakePHP why won't the name insert into the table, a null value seams to be passed as the table is updated but with out the name?
Table in Database
CREATE TABLE tests (
id serial not null unique primary key,
name varchar,
created timestamp default CURRENT_TIMESTAMP
)
add.ctp
<?php
echo $this->Form->create('Post');
echo $this->Form->input('name');
echo $this->Form->end('RSVP');
?>
Test Model
<?php
class Test extends AppModel {
var $name = 'Test';
}
TestContoller
<?php
class TestController extends AppController {
public $helpers = array('Html', 'Form');
var $name = "Test";
public function index() {
$this->set('tests', $this->Test->find('all'));
}
public function view($id = null) {
$this->Test->id = $id;
$this->set('Test', $this->Test->read());
}
public function add() {
if ($this->request->is('post')) {
if ($this->Test->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
public function edit($id = null) {
$this->Test->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Post->read();
}
else {
if ($this->Test->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Test->delete($id)) {
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
}

In your method you missed to include $this->Test->create();, which will add new row to table and inset new data to it.
public function add() {
if ($this->request->is('post')) {
$this->Test->create(); // missed this line
if ($this->Test->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash('Unable to add your post.');
}
}
}

Related

Edit in CakePHP creates new entry instead of updating old one

I tried to follow the CakePHP blog tutorial (http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/blog.html) and everything works fine, except for the edit method. The edit function as specified in the tutorial seems to work, but instead of updating the old entry, it creates a new entry in the database. (But says that it did update the old entry with the "Jop was updated" message)
My source files are:
edit.ctp
<html>
<!-- File: /app/View/Jobs/edit.ctp -->
<h1>Edit Job</h1>
<?php
echo $this->Form->create('Job');
echo $this->Form->input('type');
echo $this->Form->input('name');
echo $this->Form->input('company');
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Job');
?>
</html>
JobsController.php
class JobsController extends AppController
{
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index()
{
$this->set('jobs', $this->Job->find('all'));
}
public function view($id=null) {
if (!$id)
{
throw new NotFoundException(__('Invalid job'));
}
$job = $this->Job->findById($id);
if (!$job)
{
throw new NotFoundException(__('Invalid job'));
}
$this->set('job', $job);
}
public function add() {
if ($this->request->is('POST')) {
$this->Job->create();
if ($this->Job->save($this->request->data))
{
$this->Session->setFlash(__('Your job has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash('Unable to add your job.');
}
}
public function edit($id=null) {
if (!$id) {
throw new NotFoundException(__('Invalid job'));
}
$job = $this->Job->findById($id);
if (!$job) {
throw new NotFoundException(__('Invalid job'));
}
if ($this->request->is(array ('POST', 'PUT'))) {
$this->Job->id = $id;
if ($this->Job->save($this->request->data)) {
$this->Session->setFlash(__('Job has been updated.'));
return $this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash(__('Unable to update the job.'));
}
if (!$this->request->data) {
$this->request->data = $job;
}
}
}
public function delete($id) {
if (!$id) {
throw new NotFoundException(__('Invalid job'));
}
if ($this->request->is('GET')) {
throw new MethodNotAllowedException();
}
if ($this->Job->delete($id)) {
$this->Session->setFlash(__('Job has been deleted.'));
}
else {
$this->Session->setFlash(__('Unable to delete job.'));
}
return $this->redirect(array('action' => 'index'));
}
}
?>
If someone else has this problem, this is the solution that worked for me:
if (!$this->request->data) {
$this->request->data = $job;
}
was indented wrong, it shouldn't be within the
if ($this->request->is(array ('POST', 'PUT')))
block, but on the same level, i.e. one layer less indented.
Please check your edit.ctp page you are missing some.
<?php echo $this->Form->create('Job'); ?>
<?php
echo $this->Form->hidden('id', array('value' => $this->data['Job']['id'])); ?>

Used the Blog tutorial to create custom entry 'clients', I can't track down the issue

I originally had some basic authentication and form validation, but have taken out both of those for the sake of troubleshooting. When at /clients/add after filling out and submitting nothing happens. When doing this for the /posts/add it works correctly.
Controller/ClientsController.php
<?php
class ClientsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('clients', $this->Client->find('all'));
}
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid client'));
}
$client = $this->Client->findById($id);
if (!$client) {
throw new NotFoundException(__('Invalid client'));
}
$this->set('client', $client);
}
public function add() {
if ($this->request->is('client')) {
$this->Client->create();
if ($this->Client->save($this->request->data)) {
$this->Session->setFlash(__('Your client has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your client.'));
}
}
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid client'));
}
$client = $this->Client->findById($id);
if (!$client) {
throw new NotFoundException(__('Invalid client'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Client->id = $id;
if ($this->Client->save($this->request->data)) {
$this->Session->setFlash(__('Your client has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your client.'));
}
if (!$this->request->data) {
$this->request->data = $client;
}
}
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Client->delete($id)) {
$this->Session->setFlash(
__('The client with id: %s has been deleted.', h($id))
);
return $this->redirect(array('action' => 'index'));
}
}
}
Model/Client.php
<?php
class Client extends AppModel {
}
View/Clients/add.ctp
<h1>Add Client</h1>
<?php
echo $this->Form->create('Client');
echo $this->Form->input('contact');
echo $this->Form->input('customer');
echo $this->Form->input('state');
echo $this->Form->input('district');
echo $this->Form->end('Save Client');
?>
Here is the screenshot of database structure in case it is a db related issue.
what is $this->request->is('client') ?
please do not just copy and replace , it should be
$this->request->is('post')

how to insert values in table in cake php

here i done code for contact us page with two fileds like email,body it stores values in db
but it s not working i post the code here
Model
//model/pages.php
<?php
class pages extends AppModel{
var $useTable = 'contact';
}
?>
Controller
class PagesController extends AppController {
public $uses = array();
public function display() {
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
public function index(){
$this->set('post', $this->contact->find('all'));
}
public function create() {
if ($this->request->is('post'))
//this function for methods like get, post, set,delelte
{
// print_r('post');
$this->contact->create();
if ($this->contact->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.'));
}
}
}
view
here code for display
view/contact.ctp
<?php
echo 'welcome to Contact us';
echo $this->Form->create('pages');
echo $this->Form->input('email');
echo $this->Form->input('body', array('rows' => '6'));
echo $this->Form->end('Save pages');
?>
added in db table and values contact
Did you read something to start in CakePHP like Getting Started or CakePHP Conventions?
Reading some documentation, you will find that your tables should be named in plural and models in singular. Your model in app/Model/Contact.php looks like this:
class Contact extends AppModel {
public $useTable = 'contacts'; // It is not necessary, because CakePHP implicitly understands that your table will be in the plural
}
Now, your view located in app/View/Contacts/index.ctp:
echo $this->Form->create('Contact');
echo $this->Form->input('email');
echo $this->Form->input('body', array('rows' => '6'));
echo $this->Form->end('Save');
And your controller in app/Controller/ContactsController.php:
class ContactsController extends AppController {
public function index() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
$this->Session->setFlash(__('Your contact has been saved.'));
} else {
$this->Session->setFlash(__('Unable to add your contact.'));
}
}
}
}
This is a basic usage. I recommend you read the documentation to start with CakePHP because it has lots of interesting things that you may need.

CakePHP - Only one Query from external Model

I've two Models:
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
and
class User extends AppModel {
var $name = 'User';
var $hasMany = 'Post';
}
Now I'm having a problem with a query in the PostsController. I've an add() function and the view add.ctp which is basically a form. Now I would like to show some User information in that form.
class PostsController extends AppController {
var $name = 'Posts';
var $helper = array('Html', 'Form');
var $uses = array('User');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
function add() {
$user_id = 1;
$this->set('user', $this->User->findById($user_id));
if ($this->request->is('post')) {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
}
But now the add-view-page shows that actually two queries where triggered. So if I print_r($user) within the add-view I'm getting an array with two arrays. One for a Post with user_id = 1 and one for the actual User with id = 1. But I would like to get only the User id = 1.
Try setting recursive to false on the User model before calling findById, so you won't get any data from associated models. Like this:
function add() {
$user_id = 1;
$this->User->recursive = false;
$this->set('user', $this->User->findById($user_id));
if ($this->request->is('post')) {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}

How do I use a standalone class in cakephp 1.3?

I have a standalone class I wrote in PHP for some very basic LDAP/AD functions. and I would like to use this class in a project I am working on in cakephp.
It looks like in cakephp 1.2 I could just add the class as a vendor, however it looks like cakephp 1.3 removed support for vendors. So how would I go about calling a few function from this class?
(I'd like to try to keep the class itself the same and not turn it into a plugin, as that seems unnecessary)
Thanks!
code below:
**<?php
class UsersController extends AppController {
var $name = 'Users';
//commented out because it breaks the script
//App::import('Lib', 'ldap');
function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
function login() {
if (!empty($this->data)) {
if ($ldap->auth($this->data['User']['user'],$this->data['User']['password'])) {
$this->Session->setFlash(__('The user has been saved', true));
$this->Session->write('user', $this->data['User']['user']);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Login Failed', true));
}
}
}
function logout() {
$this->Session->delete('user');
$this->redirect($this->referer());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
$this->set('user', $this->User->read(null, $id));
}
function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
$projects = $this->User->Project->find('list');
$this->set(compact('projects'));
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
$projects = $this->User->Project->find('list');
$this->set(compact('projects'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for user', true));
$this->redirect(array('action'=>'index'));
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('User deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('User was not deleted', true));
$this->redirect(array('action' => 'index'));
}
}
?>**
Cake 1.3 still perfectly supports the idea of vendor files. In addition, they now also support "libraries", additional classes that are not 3rd party classes. Just pop your files into the /vendors or /libs directory and load the file using App::import.
I got it working, I had to call "App::import('Lib', 'ldap');" outside of the controller class and then call it as a new class inside the function I wanted.
Below is the end result
<?php
App::import('Lib', 'ldap');
class UsersController extends AppController {
var $name = 'Users';
function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
function login() {
if (!empty($this->data)) {
$ldap = new ldap;
if ($ldap->auth($this->data['User']['user'],$this->data['User']['password'])) {
if (!$this->User->findByUser($this->data['User']['user']) )
{
$ldap_info = $ldap->getInfo($this->data['User']['user']);
$this->data['User']['name'] = $ldap_info['name'];
$this->add();
}
$this->Session->write('user', $this->data['User']['user']);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Login Failed', true));
}
}
}
function logout() {
$this->Session->delete('user');
$this->redirect($this->referer());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
$this->set('user', $this->User->read(null, $id));
}
private function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
$projects = $this->User->Project->find('list');
$this->set(compact('projects'));
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
$projects = $this->User->Project->find('list');
$this->set(compact('projects'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for user', true));
$this->redirect(array('action'=>'index'));
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('User deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('User was not deleted', true));
$this->redirect(array('action' => 'index'));
}
}
?>

Categories