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));
}
Related
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;
}
}
}
I had this validation rule for a long time and it always used to work correctly, but not any more.
public $validate = array(
'email'=>array(
'email'=>array(
'rule'=>array('email', true),
'message'=>'E-mail is not correct'
),
'isUnique'=>array(
'rule'=>'isUnique',
'message'=>'This email adddress is already in use'
),
'notEmpty'=>array(
'rule'=>'notEmpty',
'message'=>'Insert email address'
)
)
I have been testing many possible ways, but none of them seem to work. Any help is much appreciated.
Here is the add function from the controller:
public function add() {
if ($this->request->is('post')) {
if( $this->request->data[ 'User' ][ 'email' ] != $this->data[ 'User' ][ 'email_confirmation' ] ) {
$this->User->invalidate( 'email_confirmation', __('Emails dont match'));
}else{
//generate password
$generatedPassword = $this->getRandomString(6);
$this->request->data['User']['password'] = $generatedPassword;
if ($this->User->save($this->request->data)) {
$name = $this->request->data['User']['name'];
$email = $this->request->data['User']['email'];
$password = $generatedPassword;
$this->Session->setFlash(__('User has been saved'), 'positive_notification');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('User has not been saved'), 'negative_notification');
}
}}}
And here is the beforeSave()
public function beforeSave($options = Array()){
if(isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
if(isset($this->data[$this->alias]['password1'])){
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password1']);
}
return true;
}
Here is the query that is being executed while saving:
INSERT INTO `schedule`.`users` (`employee_nr`, `name`, `surname`, `email`, `phone`, `address`, `post_nr`, `city`, `role`, `company_id`, `password`) VALUES ('1', 'Name', 'Surname', 'email#email.com', '11223344', '', '', '', 'employee', 54, '389be16c83bf2e65a64f727465ec0c37a77552a5')
Test isUnique by using in you controller to see why it is not working. isUnique validation rule uses isUnique model method.
try this
public $validate = array(
'email' => array(
'rule1' => array(
'rule' => 'email',
'message' => 'Please enter a valid email address.'
),
'rule2' => array(
'rule' => 'isUnique',
'message' => 'That email address exists.'
)
),
When a user enters data into my form it isn't saving to the database, this is the structure of my tables
Invoice - id, sender_id, receiver_id, template_id
Field - id, name, description, default_value, template_id
fields_invoices - id, invoice_id, field_id, entered_value
here is the invoice model
class Invoice extends AppModel{
var $name='Invoice';
var $hasMany = array(
'FieldsInvoice');
here is the field model
var $hasMany = array(
'FieldsInvoice'
);
and here is the fieldsInvoice model
<?php
class FieldsInvoice extends AppModel {
public $belongsTo = array(
'Field' => array(
'className' => 'Field',
'foreignKey' => 'field_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Invoice' => array(
'className' => 'Invoice',
'foreignKey' => 'invoice_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
here is the create function in the invoices controller
public function create($id)
{
$this->set('title_for_layout', 'Create Invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) throw new NotFoundException('Invalid ID');
$this->set('invoice_id',$id);
$names = $this->Invoice->find('list',array(
'fields'=>array('template_id'),
'conditions'=>array('id'=>$id)));
$fields = $this->Field->find('all', array(
'conditions'=>array(
'template_id'=>$names)));
$this->set(compact('fields'));
$this->set(compact('invoice_id'));
$this->set('name',$names);
$this->Invoice->create();
if(empty($this->data)){
$this->data= $this->Field->read($id);
}
else{
if($this->request->is('post'))
{
$this->Invoice->create();
if($this->FieldsInvoice->save($this->request->data, array('deep'=>true)));
{
$this->Session->setFlash('The field has been updated');
$this->redirect(array('controller'=>'invoices', 'action'=>'index'));
}
//else{
$this->Session->setFlash('Could not be saved');
//}
}
}
}
here is the view for the create function
<?php echo $this->Form->create('FieldsInvoice'); ?>
<?php foreach ($fields as $field): ?>
<?php echo $this->Form->hidden('Invoice.id'); ?>
<?php echo $this->Form->hidden($field['Field']['id']); ?>
<?php echo $this->Form->Input($field['Field']['name'], array('default' =>$field['Field']['default_value'])); ?>
<?php endforeach ;?>
<?php echo $this->Form->End('Submit');?>
when debugging the view this is the output received
array(
'Invoice' => array(
'id' => ''
),
'FieldsInvoice' => array(
(int) 5 => '',
'amount' => 'test',
(int) 6 => '',
'description' => 'test1',
(int) 7 => '',
'totalacctowing' => 'test2',
(int) 8 => '',
'pmtinfo' => 'test3'
)
)
Instead of:
$this->Invoice->save($this->request->data);
in your if condition use the following syntax;
$this->Invoice->save($this->request->data, array('deep' => true));
This link will guide you to save associated data.
Have you tried debugging? Try viewing your form data and see if all fields are ok. Use debug() or pr().
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
I have the model AccountLicense, when I execute the method getExpiringLicenes for some reason the association I have with the parent model (AccountUser) the primary key of AccountUser is not being used, it is instead using the default primary key 'id'. I am not sure why this is happening. This is all part of a plugin.
Any help with this is greatly appreciated.
This is the exception I am getting:
Warning (512): SQL Error: 1054: Unknown column 'AccountUser.id' in 'on clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
This is the query that is being executed:
SELECT `AccountLicense`.`license_id`, `AccountLicense`.`user_id`,
`AccountLicense`.`board_id`, `AccountLicense`.`license_number`,
`AccountLicense`.`license_state`, `AccountLicense`.`license_designation_id`,
`AccountLicense`.`active_date`, `AccountLicense`.`expire_date`,
`AccountLicense`.`is_active`, `AccountLicense`.`is_confirmed`,
`AccountLicense`.`is_primary`, `AccountUser`.`user_id`, `AccountUser`.`user_name`,
`AccountUser`.`user_pass`, `AccountUser`.`user_status`, `AccountUser`.`user_group`,
`AccountUser`.`instance_id`, `AccountUser`.`is_logged_in`, `AccountUser`.`is_visible`,
`AccountUser`.`created_by`, `AccountUser`.`last_modified_by`,
`AccountUser`.`created_date`, `AccountUser`.`last_modified_date`
FROM `account_licenses` AS `AccountLicense`
LEFT JOIN `account_users` AS `AccountUser`
ON (`AccountLicense`.`user_id` = `AccountUser`.`id`)
WHERE `AccountLicense`.`expire_date` BETWEEN '2011-10-05' and '2011-11-04'
This is my AccountLicenses model:
<?php
class AccountLicense extends AppModel {
var $name = 'AccountLicense';
var $primaryKey = 'license_id';
var $plugin = 'AccountModule';
var $belongsTo = array(
'AccountUser' => array(
'className' => 'AccountUser',
'foreignKey' => 'user_id'
)
);
public function getExpiringLicenses($date = null)
{
if(is_null($date))
$date = date('Y-m-d',strtotime("+30 days"));
return $this->find(
'all',
array(
'conditions' => array(
$this->name . '.expire_date BETWEEN ? and ?' =>array(date('Y-m-d'),$date)
)
)
);
}
}
?>
This is my AccountUser model:
<?php
class AccountUser extends AppModel {
var $name = 'AccountUser';
var $primaryKey = 'user_id';
var $actsAs = array('Containable');
var $validate = array(
'user_name'=>array(
'rule'=>'isUnique',
'message'=>'This username has already been taken. Please try again'
),
'user_pass' => array(
'rule' => array('between', 8, 16),
'message' => 'Passwords must be between 8 and 16 characters long.')
);
var $hasMany = array(
'AccountLicense' => array(
'className' => 'AccountLicense',
'foreignKey' => 'user_id'
)
);
?>
Since is a plugin your association should have the plugin name
Example:
If your plugin name is AccountModule your association should look like
var $belongsTo = array(
'AccountUser' => array(
'className' => 'AccountModule.AccountUser',
'foreignKey' => 'user_id'
)
);
Also is not wrong, but the correct way to declare a model class inside a plugin is
<?php
class AccountLicense extends AccountModuleAppModel {
If it is in the correct place in your folder structure and correctly declared you won't need this line
var $plugin = 'AccountModule';