CakePHP ACL tutorial group_id - php

I am following the
http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html tutorial and I am at the part where you add the group and user accounts...
However, when I access to add new user, the drop down for the group is is empty... What can be wrong?
here is how User.php model looks like
<?php
App::uses('AppModel', 'Model');
/**
* User Model
*
* #property Group $Group
* #property Image $Image
*/
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
/**
* Display field
*
* #var string
*/
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $actsAs = array('Acl' => array('type' => 'requester'));
public function beforeSave() {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
public function bindNode($user) {
return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
}
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Here is how Group.php model looks like
<?php
App::uses('AppModel', 'Model');
/**
* Group Model
*
* #property User $User
*/
class Group extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* #var array
*/
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* #var array
*/
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'group_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
my tables look like this
'users' table
> > Field Type Null Key Default Extra
> > id int(11) NO PRI NULL auto_increment
> > username varchar(64) NO UNI NULL password varchar(82) NO NULL
> > first_name varchar(64) NO NULL last_name varchar(64) NO NULL
> > created datetime YES NULL group_id int(11) NO NULL
'groups' table
Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
name varchar(100) NO NULL
created datetime YES NULL
modified datetime YES NULL
View/Users/add.ctp code
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('group_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index'));?></li>
<li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Images'), array('controller' => 'images', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Image'), array('controller' => 'images', 'action' => 'add')); ?> </li>
</ul>
</div>
I created 2 groups. One is "visitors" and one is "admin"..
here is how aros table looks like right now now
id parent_id model foreign_key alias lft rght
2 NULL Group 4 1 2
3 NULL Group 5 3 4

Have you added group first? First you have to add the group then try adding the user.
Please verify you have a belongsTo relationship setup in the user model User.php
public $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Check your UserController Add method has the following code
Please verify the $groups = $this->User->Group->find('list');
$this->set(compact('groups')); part
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));
}

Related

CakePHP Model "Member" is not associated with model "Member"

I am making a PHP forum with CakePHP I am troubles with getting an array of all the members then echoing them in a view, here is my code.
<?php
App::uses('AppModel', 'Model');
class Member extends AppModel {
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty')
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty')
),
),
'email' => array(
'email' => array(
'rule' => array('email')
),
),
);
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
That is my member model here is my MembersController
<?php
App::uses('Controller', 'AppController');
class MembersController extends AppController {
public $components = array('Paginator');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('profile','login');
}
public function index(){
$this->Paginator->settings['contain'] = array('Member');
$this->set('members', $this->Paginator->paginate());
}
public function profile($id=null) {}
public function login() {
if($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
And here is my Index view
<div class="row">
<?php foreach ($members as $member): ?>
<?php echo $user[name]; ?>
<?php endforeach; ?>
</div>
When I access example.com/members I get an error saying Model "Member" is not associated with model "Member" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]
Before you ask I have made AppModels actas Containable
class AppModel extends Model {
public $actsAs = array('Containable');
}
Remove that line:
$this->Paginator->settings['contain'] = array('Member');
You're causing a self-join by using this and you haven't defined that in your members model and you dont want that.
This is wrong as well plus your php has a syntax error - missing ' around the name.
<?php foreach ($members as $member): ?>
<?php echo $user[name]; ?>
<?php endforeach; ?>
It should be:
<?php foreach ($members as $member): ?>
<?php echo $member['Member']['name']; ?>
<?php endforeach; ?>
You're struggling with the very basics, I would recommend you to do the blog tutorial or at least read about how data is passed to the view and how model associations work. You can find everything in the book.
Your problem is here:
public function index(){
$this->Paginator->settings['contain'] = array('Member');
$this->set('members', $this->Paginator->paginate());
}
Since paginate is being called from the members controller, it will by default try to paginate Member. Your contain setting is indicating the Member data should also contain the associated model, also named Member. You have no association between Member and itself, hence the error. I'm going to assume that contain setting was a mistake, so simply remove the line $this->Paginator->settings['contain'] = array('Member');
$this->Paginator->settings['contain'] = array('Member'); is your problem. Since the setting are going to be passed as options to find() call on Member model you are effectively asking Member to contain itself. Set 'contain' to array() to prevent any associated records from being fetched.
Though the recommended way it to set property $recursive to -1 in AppModel so that by default no associated records are fetched. Containable is then used to fetch associated records as required.

Multiple select in input Cakephp

WHen i have an input field with the multiple options concerning 'customer_id' the data request generated is
$request->data containing array(CompanysCustomer => array('id' => '1', 'uid' => 'fhs32hrqwr8wfsdiof', 'customer_id' => array('0' => '5', '1' => '8', '2' => '9')).
I dont undestand how to use or make use of this array. It Works well when i dont use multiple, because then i dont get array at 'customer_id'. I have an idea to manipulate the array and list them out ond do the save pr foreach, but that seems unefficient, it has to be some other way?
And im having trouble saving this to the db. I managed to make it when i have one single record to save. I cant make it save many records at once.
Version 2.4.1
public function add($id) {
if ($this->request->is('post')) {
$this->CompanysCustomer->create();
if ($this->CompanysCustomer->save($this->request->data)){
$this->Session->setFlash(__('The Companys Customer has been saved.'));
return $this->redirect(array('action' => 'index'));
}
else{
$this->Session->setFlash(__('The Companys Customer could not be saved. Please, try again.'));}
}
$customers = $this->CompanysCustomer->Customer->find('list');
$this->set(compact('customers'));
}
Model CompanysCustomer
<?php
App::uses('AppModel', 'Model');
/**
* CompanysCustomer Model
*/
class CompanysCustomer extends AppModel {
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Company' => array(
'className' => 'Company',
'foreignKey' => 'company_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
View: CompanyCustomer\add.ctp
<div class="companysCustomers form">
<?php echo $this->Form->create('CompanysCustomer'); ?>
<fieldset>
<legend><?php echo __('Add Companys Customer'); ?></legend>
<?php
$company_id = $id;
echo $this->Form->input('company_id', array(
'type' => 'hidden',
'value' => $id,
));
$uid = uniqid("", $more_entropy = true);
echo $this->Form->input('uid', array(
'type' => 'hidden',
'value' => $uid,
));
//echo $this->Form->input('company_id');
echo $this->Form->input('customer_id',array('type' => 'select', 'multiple' => 'checkbox','size' => '20'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
Reading the documentation helps
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
'multiple' => true
for example.
For saving use
saveMany()

CakePHP SaveAssociated / SaveAll ignoring fields

I am having an issue where CakePHP is ignoring saving a field I specify and instead creating extra records. I am using CakePHP 2.3.6.
My table looks like this:
CREATE TABLE `events_guests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guest_id` int(11) NOT NULL,
`event_id` int(11) NOT NULL,
`promoter_id` int(11) DEFAULT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`attended` varchar(15) NOT NULL DEFAULT 'No',
PRIMARY KEY (`id`)
)
Heres the code
public function addGuest($event_id, $promoter_id = null) {
if ($this->request->is('post')) {
$this->Guest->create();
$event_data = array('event_id' => $event_id);
$data = $this->request->data;
$data['Event'] = $event_data;
if($promoter_id) {
$data['Event']['promoter_id'] = $promoter_id;
}
if ($this->Guest->saveAssociated($data)) {
$this->Session->setFlash(__('The guest has been added to the guestlist'), 'flash/success');
} else {
$this->Session->setFlash(__('The guest could not be saved. Please, try again.'), 'flash/error');
}
Here is my data that I am trying to save:
Array (
[Guest] => Array
(
[first_name] => Joe
[last_name] => Schmoe
)
[Event] => Array
(
[event_id] => 1
[promoter_id] => 2
)
)
My Models follow:
class Guest extends AppModel {
public $hasAndBelongsToMany = array(
'Event' => array(
'className' => 'Event',
'joinTable' => 'events_guests',
'foreignKey' => 'guest_id',
'associationForeignKey' => 'event_id',
'unique' => 'true',
),
'Promoter' => array(
'className' => 'Promoter',
'joinTable' => 'events_guests',
'foreignKey' => 'guest_id',
'associationForeignKey' => 'promoter_id',
'unique' => 'true',
)
);
}
And
class Event extends AppModel {
public $hasAndBelongsToMany = array(
'Guest' => array(
'className' => 'Guest',
'joinTable' => 'events_guests',
'foreignKey' => 'event_id',
'associationForeignKey' => 'guest_id',
'unique' => 'keepExisting',
'order' => 'last_name',
),
'Promoter' => array(
'className' => 'Promoter',
'joinTable' => 'events_promoters',
'foreignKey' => 'event_id',
'associationForeignKey' => 'promoter_id',
'unique' => 'keepExisting',
)
);
}
The results from this are 2 records for EventsGuests, neither with a promoter_id.
One record receives event_id = 1 AS EXPECTED
Other record receives event_id = 2, which is actually the promoter_id
I have tried using a mix of saveAssociated and saveAll
Any help is greatly appreciated! Thank you!
Reference: Cakephp save extra attribute in HABTM relation
For your problem, don't use HABTM, because, HABTM is normally use for connect 2 tables.
So, if you have more than 2 fields, create a model and use it like belongsTo and hasMany relationships.
class Event extends AppModel {
$hasMany = array(
'EventGuest' => array(
'className' => 'EventGuest',
'foreignKey' => 'event_id'
)
);
}
class Guest extends AppModel {
$hasMany = array(
'EventGuest' => array(
'className' => 'EventGuest',
'foreignKey' => 'guest_id'
)
);
}
class Promoter extends AppModel {
$hasMany = array(
'EventGuest' => array(
'className' => 'EventGuest',
'foreignKey' => 'promoter_id'
)
);
}
Now, your relationship model.
class EventGuest extends AppModel {
$belongsTo = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'event_id'
),
'Guest' => array(
'className' => 'Guest',
'foreignKey' => 'guest_id'
),
'Promoter' => array(
'className' => 'Promoter',
'foreignKey' => 'promoter_id'
)
);
}
And continue use your code.
public function addGuest($event_id, $promoter_id = null) {
if ($this->request->is('post')) {
$this->Guest->create();
$event_data = array('event_id' => $event_id);
$data = $this->request->data;
$data['Event'] = $event_data;
if($promoter_id) {
// EDITED
$data['Promoter']['promoter_id'] = $promoter_id;
}
if ($this->Guest->saveAssociated($data)) {
$this->Session->setFlash(__('The guest has been added to the guestlist'), 'flash/success');
} else {
$this->Session->setFlash(__('The guest could not be saved. Please, try again.'), 'flash/error');
}
}
}

Simple Acl Controlled Application - Not allowing me to add new user

I'm new to CakePHP and I'm trying to implement the Simple Acl Controlled Application tutorial, and I've reached the part where you try to add new users and groups..
I successfully added the groups but when i try and add new users I receive the "The user could not be saved. Please, try again." Part of the function.
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
I noticed that the form tries to create a drop down box of all the different groups that I created but the drop box is empty and I have created three different groups (Admin, Responder and Volunteer).
Here is a copy of the add user view..
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('group_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
</ul>
</div>
Model as requested:
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $belongsTo = array('Group');
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public $primaryKey = 'id';
public $validate = array(
'id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'password' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'group_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
//old belongs to
// public $belongsTo = array(
// 'Group' => array(
// 'className' => 'Group',
// 'foreignKey' => 'group_id',
// 'conditions' => '',
// 'fields' => '',
// 'order' => ''
// )
// );
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public function beforeSave($options = array()) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
}
Debug message:
array(
'User' => array(
'password' => '*****',
'id' => '',
'username' => 'iwanjones'
)
)
Any help would be appreciated.
Thanks
In the form within your view, you create the id field as an input field (although through Cake's automagic it should convert it to a hidden input). When creating a new user, there is no id yet. It will be determined upon saving (creating) the record. In most applications this will be done by the MySQL backend's AUTO_INCREMENT functionality, which picks the first available "free" id.
In case of adding a new user, the id field is therefor not necessary. You only need it when you want to edit an existing user and make sure Cake edits the proper user, by setting it's id.
At this moment you set the id field in your view, but it gets no value, since it's a new user. In your Model you have added a validation rule that requires the id field to be numeric. But, the value is actually empty. You should do two things to get this working:
Drop the echo $this->Form->input('id'); line from the add view.
Remove the validation rule for the id field from your model (it's pretty uncommon to validate your primary key field, as Cake already handles this the proper way).
This should allow for the user to be saved successfully.

Model not saved to database

CREATE TABLE IF NOT EXISTS `web_subjects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`web_subject_category_id` int(11) DEFAULT NULL,
`title` varchar(128) DEFAULT NULL,
`type` varchar(128) DEFAULT NULL,
`description` text,
`description_long` text,
`editable` int(1) DEFAULT NULL,
`deletable` int(1) DEFAULT NULL,
`published` int(1) DEFAULT NULL,
`order_number` int(11) DEFAULT NULL,
`created` timestamp NULL DEFAULT NULL,
`modified` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
Model
class WebSubject extends AppModel
{
public $name = "WebSubject";
public $belongsTo = array("WebSubjectCategory");
public $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => "Completati denumirea!"
)
);
public $hasMany = array(
'Image' => array(
'className' => 'WebFile',
'foreignKey' => 'entity_id',
'conditions' => array(
'Image.type' => 'image',
'Image.entity_table_name' => 'web_subjects'
),
'order' => array('Image.order_number ASC', 'Image.id DESC'),
'dependent' => true
),
'Video' => array(
'className' => 'WebFile',
'foreignKey' => 'entity_id',
'conditions' => array(
'Video.type' => 'video',
'Video.entity_table_name' => 'web_subjects'
),
'order' => array('Video.order_number ASC', 'Video.id DESC'),
'dependent' => true
)
);
}
Controller Action
public function admin_page_add(){
if(!empty($this->request->data))
{
$this->WebSubject->create($this->data["WebSubject"]);
$this->WebSubject->type = 'page';
//debug($this->WebSubject);
if($this->WebSubject->save()){
//debug($this->WebSubject);
//die(0);
$this->Session->setFlash("Pagina a fost salvata!", "flash/simpla_success");
$this->redirect('pages');
}
else{
$this->Session->setFlash("Pagina NU a fost salvata!", "flash/simpla_error");
}
}
}
The problem is that the model appeares to be saved and I am redirected as expected, but it doesn't get inserted into the database.
Using debug(Model) I saw that the id that the model is taking is incremented (like it is inserted and then deleted).
I used sql_dump - no trace of INSERT.
And, of course, no validation errors.
What am I missing ?
User this
This is my model
<?php
App::uses('AppModel', 'Model');
/**
* CarModel Model
*
* #property Manufacturer $Manufacturer
* #property Showroom $Showroom
*/
class CarModel extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'model';
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'manufacturer_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'model' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Manufacturer' => array(
'className' => 'Manufacturer',
'foreignKey' => 'manufacturer_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'Showroom' => array(
'className' => 'Showroom',
'foreignKey' => 'car_model_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'RequestCar' => array(
'className' => 'RequestCar',
'foreignKey' => 'car_model_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
?>
My controller
/**
* admin_add method
*
* #return void
*/
public function admin_add() {
$this->layout = 'admin_layout';
if ($this->request->is('post')) {
$this->CarModel->create();
if ($this->CarModel->save($this->request->data)) {
$this->Session->setFlash(__('The car model has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car model could not be saved. Please, try again.'));
}
}
$manufacturers = $this->CarModel->Manufacturer->find('list');
$this->set(compact('manufacturers'));
}
$this->WebSubject->save();
saves nothing as there is no data! (Edit : this statement is wrong as said in comments.)
Put:
$this->WebSubject->save($this->request->data);
save() function docs

Categories