Inserting with empty string in cakePHP - cakephp-2.0

i am new in cakePHP framework with version 2.0 my problem is when i save the data or insert the new record the field is empty or no data to be save how can i fix this. .
My Model
class Post extends AppModel{
public $name = 'posts';
}
My Controller
public function add(){
if($this->request->is('post')){
$this->Post->create();
if($this->Post->save($this->request->data)){
$this->Session->setFlash('The posts was saved');
$this->redirect('index');
}
}
}
My View
echo $this->Form->create('Create Posts');
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Save Posts');

You need to put the validation rules in the Post model, Then you can check validate data or not in the controller action before save into model. See the following Model and controller
In Model
class Post extends AppModel{
public $name = 'posts';
public $validate = array(
'title' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'This is can'\t blank'
),
),
'body' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'This is can'\t blank'
),
),
);
}
In Controller
public function add(){
if($this->request->is('post')){
$this->Post->create();
$this->Post->set($this->request->data);
if ($this->Post->validates()) {
// it validated logic
if($this->Post->save($this->request->data)){
$this->Session->setFlash('The posts was saved');
$this->redirect('index');
}
} else {
// didn't validate logic
$errors = $this->Post->validationErrors;
}
}
}

Related

Save data in two tables in CakePHP

I have a simple form in Cake :
I want to save the informations in the users and companies tables .
Users Table :
Companies table :
I'm in need to get two sql queries .
First of all :
- Insert the new users with a new company id which will be created in companies table .
For instance with this case, testza,
users : (new entry)
testza password company id (new if of the new company entry)
companies (new entry) :
Id (3) Creaz (name) 500 000 (revenue_generated) .
I guess i'm clear :
companies/signup (view)
<div class="page-header">
<h1>Company Registration</h1>
</div>
<?php echo $this->Form->create('Company'); ?>
<?php echo $this->Form->input('User.username',array('label'=>'Login')); ?>
<?php echo $this->Form->input('User.password',array('label'=>'Password')); ?>
<?php echo $this->Form->input('User.passwordconfirm',array('label'=>'Confirm password','type'=>'password')); ?>
<hr>
<?php echo $this->Form->input('Company.company_id',
array(
'label'=>'Company',
'options' => $companies,
'empty' => 'Select Company Name'
));
?>
<?php echo $this->Form->input('Company.revenue_generated',array('label'=>'Revenue Generated')); ?>
<?php echo $this->Form->end('Envoyer'); ?>
the action signup of the Controller CompaniesController
<?php
class CompaniesController extends AppController{
public $uses = array('User');
public function signup() {
$this->loadModel('Company');
$companies = $this->Company->find('list'); //we get the authors from the database
$this->set('companies', $companies);
if($this->request->is('post') || $this->request->is('put') ){
$d = $this->request->data['User'];
$c = $this->request->data['Company'];
if($d['password'] != $d['passwordconfirm']){
$this->Session->setFlash("Les mots de passes ne correspondent pas","notif",array('type'=>'error'));
}
else{
if(empty($d['password']))
unset($d['password']);
if($this->User->save($d) || $this->Company->save($c)){
$this->Session->setFlash("L'utilisateur a bien été enregistré","notif");
}
}
}
}
..............
User Model :
<?php
class User extends AppModel{
public $actsAs = array('Containable');
public $belongsTo = array(
'Town' => array(
'className' => 'Town'
),
'Company' => array(
'className' => 'Company'
)
);
public $recursive = -1;
public $order = 'User.id DESC';
public $validate = array(
'username' => array(
'rule' => 'isUnique',
'allowEmpty' => false,
'message' => "Ce nom d'utilisateur est déja pris"
),
'password' => array(
'rule' => 'notEmpty',
'message' => "Vous ne pouvez pas entrer de mot de pase"
)
);
public function beforeSave($options = array()){
if(!empty($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
Company Model :
<?php
class Company extends AppModel{
public $validate = array(
'revenue_generated' => array(
'rule' =>'notEmpty',
'message' => 'Enter Your Generated Revenue.',
'required' => true
)
);
}
Since you are going to use an existing company already in the database from the drop down menu, try saving both models at the same time with Model::saveAssociated(). This will save both records at once for you, and insert the Company id, automatically into the foreign key for the users table: User.company_id on successful save.
Replace your two save() function calls with one call to saveAssociated():
if($this->User->saveAssociated($data)) {
//do something after save.
}
Here is a complete example using your existing code:
$this->set('companies', $this->Company->find('list'));
if($this->request->is(array('post', 'put'))) {
//Dont need to seperate this anymore
//$d = $this->request->data['User'];
//$c = $this->request->data['Company'];
if($this->request->data['User']['password'] != $this->request->data['User']['passwordconfirm']){
$this->Session->setFlash("Les mots de passes ne correspondent pas","notif",array('type'=>'error'));
} else {
if(empty($this->request->data['User']['password']))
unset($this->request->data['User']['password']);
if($this->User->saveAssociated($this->request->data)){
$this->Session->setFlash("L'utilisateur a bien été enregistré","notif");
}
}
}
The Company model is also missing the hasMany relationship for User. Add this to Company model:
public $hasMany = array('User');
Refer to Cake Manual here for documentation on SaveAssociated():
http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
here is the solution i suggest and i work with, start by saving the company first to generat the id the save the user :
if($this->Company->save($this->request->data['Company'])){
$this->Company->User->save($this->request->data['User']); // if save doesn't work try $this->Company->User->saveAll($this->request->data['User']);
...
}
update :
add the hasmany relation in the company model :
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'company_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
hope ut helps.

Saving Associated Models with CakePHP

I'm trying to build on the example code at http://book.cakephp.org/2.0/en/models/saving-your-data.html in the section that starts off with "Saving Related Model Data (hasOne, hasMany, belongsTo)". However, I'm getting the following error message when I call unset:
Indirect modification of overloaded property AppModel::$MealContent has no effect [APP\Controller\MealsController.php, line 15]
Attempt to modify property of non-object [APP\Controller\MealsController.php, line 15]
Naturally, the data isn't saved either.
As you can see, my application doesn't have Companies or Accounts. Instead, I have Meals and MealContents but the relationships seem to have been set up the same. Obviously, there's a problem somewhere though so here's some code.
Meals.php:
class Meals extends Entry {
public $hasMany = 'MealContents';
public $validate = array(
'Timestamp' => array(
'validDate' => array(
'rule' => array('garbageDateChecker'),
'required' => true,
'message' => 'The date could not be understood.'),
'noTimeTravel' => array(
'rule' => array('noTimeTravel'),
'required' => true,
'message' => 'You cannot enter times in the future.')
)
);
}
MealContents.php:
class MealContents extends Entry {
public $belongsTo = 'Meals';
public $validate = array(
'Meals_ID' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'This field cannot be blank')
),
'Item_ID' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'This field cannot be blank')
)
);
}
And finally, the controller's index() function:
public function index() {
$this->set('title_for_layout', "Food Log");
if ($this->request->is('post')) {
$this->Meal->create();
unset($this->Meal->MealContent->validate['Meals_ID']);
if ($this->Meal->saveAssociated($this->request->data)) {
$this->Session->setFlash('The meal was logged.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash("Couldn't save the meal.");
}
}
}
Entry.php
abstract class Entry extends AppModel {
public function garbageDateChecker($dateToCheck) {
date_default_timezone_set("America/Tijuana");
$result = TRUE;
try {
new DateTime($dateToCheck['Timestamp']);
} catch (Exception $e) {
$result = FALSE;
}
return $result;
}
public function noTimeTravel($dateToCheck) {
date_default_timezone_set("America/Tijuana");
$result = TRUE;
$objectifiedDateToCheck = new DateTime($dateToCheck['Timestamp']);
$currentTime = new DateTime("now");
if ($objectifiedDateToCheck > $currentTime) {
$result = FALSE;
}
return $result;
}
}
I'm pretty sure the save() failure isn't due to the validation because even when I comment out the validation rule and the unset() line, the data isn't saved.
It would be easy to blame it on bad data or a screwed up view. However, the data looks OK:
$this->request->data
--MealContents
---[0]
-----[Food_Item_ID] = "0"
--Meals
---[Comments] = ""
---[Timestamp]
-----[day] = "12"
-----[hour] = ...
What have I missed when I read the CakePHP book?
In all of your model class declarations, you should be extending the AppModel class not "Entry". Also, you need to change your model names to singular nouns. Meal instead of Meals.
class Meal extends AppModel {
//your model's code
}
class MealContent extends AppModel {
//your model's code
}
In your controller, if you would like to skip validation on the saveAssociated call, you can pass an options array, with element "validate" set to False as the second parameter. You should never use unset on your model like that as it will affect the rest of your app.
$this->Meal->saveAssociated($this->request->data, array("validate" => false));

linking tables cakephp

Hi all I finally got my validation for my invoices model working 100% but now its thrown off my validation in relationship model(which was working) because now it references everything in the wrong table.
The relationship model is supposed to make sure that a user exists in the users table before you can send them a request.
The invoice model is supposed to make sure that a user has a column with another user in the the relationship table.
how can I change it to make it work properly? At the moment the code completely throws off my entire relationship side of the website.
-relationship model
class Relationship extends AppModel
{
var $name = 'Relationship';
public $useTable = 'relationships_users';
public $primaryKey = 'id';
public $hasMany = array(
'Invoice' =>
array(
'className' => 'Invoice',
'joinTable' => 'invoice',
'foreignKey' => 'invoice_id'));
public $belongsTo = array(
'User' =>array(
'className' => 'User',
'foreignKey' =>'partyone','partytwo',
'associationForeignKey' => 'username',
));
var $validate = array(
'date' => array(
'rule' => array(
'datevalidation',
'systemDate'
),
'message' => 'Current Date and System Date is mismatched'
),
'partytwo' => array(
'userExists' => array(
'rule' => array(
'userExists',
),
'message' => 'That username doesnt exist.'
),
),
);
function datevalidation($field = array(), $compare_field = null)
{
if ($field['date'] > $compare_field)
return TRUE;
else
return FALSE;
}
function userExists($check)
{
$userExists = $this->User->find('count', array('conditions' => array('User.username'=>$check)));
if ($userExists == 1) {
return TRUE;
}
else
return FALSE;
}
-user model
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel{
public $name = 'User';
public $hasMany = array(
'Relationship' =>
array(
'className' => 'Relationship',
'joinTable' => 'relationships_users',
'foreignKey' => 'id',
'unique' => false,));
public $useTable = 'users';
public $primaryKey = 'id';
public $validate = array(
'username'=>array(
'The username must be between 5 and 15 characters.'=>array(
'rule'=>array('between', 5, 15),
'message'=>'The username must be between 5 and 15 characters.'
),
'That username has already been taken'=>array(
'rule'=>'isUnique',
'message'=>'That username has already been taken.'
)),
'email'=>array(
'Valid email'=>array(
'rule'=>array('email'),
'message'=>'Please enter a valid email address'
)
),
'password'=>array(
'Not Empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter your password'
)
),
'Match passwords'=>array(
'rule'=>'matchPasswords',
'message'=>'Your passwords do not match'
)
,
'password_confirmation'=>array(
'Not Empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please confirm your password'
)));
public function matchPasswords($data){
if ($data['password']==$this->data['User']['password_confirmation']){
return true;
}
$this->invalidate('password_confirmation','Your passwords do not match');
return false;
}
public function beforeSave(){
if(isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
invoice model-
class Invoice extends AppModel{
var $name='Invoice';
//public $useTable = 'invoices';
//public $primaryKey = 'id';
public $belongsTo = array(
'Relationship' =>array(
'className' => 'Relationship',
'foreignKey' =>'relationship_id',
)
);
var $validate = array(
'to' => array(
'relationshipExists' => array(
'rule' => array(
'relationshipExists',
),
'message' => 'sorry you dont have a relationship with that user.'
),
),
);
var $validateTwo = array(
'datecreated'=>array(
'dateHasntExpired' => array(
'rule'=>array(
'dateHasntExpired',
),
'message'=> 'sorry but your relationship has expired.'
),
),
);
public function relationshipExists($check){
$relationshipExists=$this->Relationship->find('count', array(
'conditions' => array(
'Relationship.partyone <>' => current($check),
'Relationship.partytwo' => current($check),
// 'Relationship.active'==true,
// get the value from the passed var
)
));
if ($relationshipExists == true) {
return TRUE;
}
else
return FALSE;
}
public function dateHasntExpired($check){
$dateHasntExpired=$this->Relationship->find('count', array(
'conditions'=>array(
'DATE(Relationship.expirydate) > DATE(Invoice.datecreated)',
)));
if ($dateHasntExpired == true) {
return TRUE;
}
else
return FALSE;
}
}
here is the error that comes up when trying to view current relationship requests
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Invoice.invoice_id' in 'field list'
SQL Query: SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate`, `Invoice`.`invoice_id` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`invoice_id` IN (97, 98, 99, 101, 104, 105)
Notice: If you want to customize this error message, create
app\View\Errors\pdo_error.ctp
and here is the error i get when trying to view the relationship requests ive sent
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Invoice.invoice_id' in 'field list'
SQL Query: SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate`, `Invoice`.`invoice_id` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`invoice_id` IN (97, 98, 99, 101, 104, 105)
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
needed to use the unbind method in my controllers, here is an example of the unbind method I used throughout my relationship controller.
public function approve($id=null){
$this->set('title_for_layout', 'Relationships');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
$this->Relationship->unbindModel(array('hasMany'=>array('Invoice')));
if ($this->request->is('get')) {
$this->request->data = $this->Relationship->read(NULL, $id);
} else {
//sets active to 1
$this->Relationship->read(null, $id);
$this->Relationship->set(array('active' => true,));
if ($this->Relationship->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'request'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
and because this threw errors in my invoice controller I used unbind there as well and here is an example of the unbind I used in invoicescontroller
public function viewInvoice($id = NULL){
$this->set('title_for_layout', 'View invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->Invoice->unbindModel(array('belongsTo'=>array('Relationship')));
$this->set('invoice', $this->Invoice->read(NULL, $id));
}

Lots of Login Code

This is a fairly long question but I have know idea where it's going wrong. I am making an ajax login script using CakePHP 2.0 but it keeps failing. I will post all of my code, and i hope someone has the time to go through it.
This is my sql Database
AccountID AccountEmail AccountPassword AccountActive
1 chris#hotmail.co.uk pass 0
2 chris#gmail.com pass 1
This is my relevant Model Code
class Account extends AppModel {
public $name = 'Account';
public $validate = array(
'AccountEmail' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please Enter A Valid Email.'
),
'email' => array(
'rule' => array('email', true),
'message' => 'Please supply a valid email address.'
)
),
'AccountPassword' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please Enter A Valid Password.'
)
)
);
}
This is my relevant Controller Code
class AppController extends Controller {
/**
* Class Variables
*/
public $helpers = array('Js', 'Html', 'Session', 'Form');
public $components = array(
'Session',
'RequestHandler',
'Auth' => array(
'logoutRedirect' => array(
'controller' => 'Accounts',
'action' => 'login'
),
'authError' => 'You can\'t Access That Page',
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'AccountEmail',
'password' => 'AccountPassword'
),
'scope' => array('AccountActive' => '1')
)
)
)
);
}
class AccountsController extends AppController {
/**
* Class Variables
*/
public $name = 'Accounts';
public $layout = 'Accounts';
/**
* Class Functions
*/
public function login()
{
if ($this->request->is('ajax')) {
$this->Account->set($this->data);
if ($this->Account->validates()) {
if($this->Auth->login()) {
echo "logged In";
exit;
} else {
echo "Login Failed";
exit;
}
} else {
echo 'validation/' . json_encode($this->Account->invalidFields());
exit;
}
}
}
I don't think there is anything else. Again i'm sorry for the huge amount of code but i just don't know what you need.
The info is all passed via 'echo' to jquery which at the moment is just displaying the response via 'alert'.
I know the validation is working, but if i enter the info of someone who should be able to login it just shows "Login Failed". Thanks For Your Time.
Your passwords in the database need to be in their hashed form. Using Cake's default settings, 'pass' would be: 1c31af5bd9913ff511fe780f506e6fab68979b90

Cakephp registration problem

I followed tutorial on CakephpTV to make registration by AuthComponent. Yesterday everything worked good, but today it isn't.
When I'm trying to add new account it says that passwords do not match, and in password field there is hashed password and in password confirmation there is normal (not hashed) password.
My UserController class:
class UsersController extends AppController
{
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('new_user');
if($this->action == 'add' || $this->action == 'edit') {
$this->Auth->authenticate = $this->User;
}
}
function new_user()
{
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Rejestracja zakończona pomyślnie');
$this->redirect(array('action' => 'index'));
}
}
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
My User model:
class User extends AppModel {
var $name = 'User';
var $validate = array (
'email' => array(
'Please supply a valid email address.' => array(
'rule' => 'email',
'message' => 'Please supply a valid email address.'
),
'Already exists' => array(
'rule' => 'isUnique',
'message' => 'Must be unique'
)
),
'password' => array (
'aThis field must have between 6 and 40 alphanumeric characters.' => array(
'rule' => '/^[^\'"]{6,40}$/i',
'message' => 'aThis field must have between 6 and 40 alphanumeric characters.'
),
'Do not match' => array(
'rule' => 'matchPasswords',
'message' => 'Do not match'
)
)
);
function matchPasswords($data) {
if($data['password'] == $this->data['User']['password_confirmation']) {
return TRUE;
}
$this->invalidate('password_confirmation', 'DO not match');
return FALSE;
}
function hashPasswords($data) {
if(isset($this->data['User']['password'])) {
$this->data['User']['password'] = Security::hash($this->data['User']['password'], NULL, TRUE);
return $data;
}
return $data;
}
function beforeSave() {
$this->hashPasswords(NULL, TRUE);
$this->data['User']['registration_date'] = date("Y-m-d H:i:s");
return TRUE;
}
}
My new_user.ctp:
echo $this->Form->create('User', array('action' => 'new_user'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('password_confirmation',array('type' => 'password'));
echo $this->Form->end('Zarejestruj');
My AppController class:
class AppController extends Controller {
var $components = array('Auth','Session');
function beforeFilter() {
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
$this->Auth->allow('index','view','display','new_user');
$this->Auth->authError = 'Zaloguj się aby zobaczyć tą stronę.';
$this->Auth->loginError = 'Wprowadzono niepoprawne dane.';
$this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home');
$this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home');
}
}
CakePHP automatically hashes fields named password in a User model. This is why you get an already hashed password back when the form fails. You might want to check if the hash you get back is the same hash as you'd expect to get back.
Apart from that, in your function matchPasswords your checking on $this->data['password'] whereas I think that should be $this->data['User']['password'].

Resources