http://webdesign4.georgianc.on.ca/~100141468/comp2084/todo/clients
http://bin.cakephp.org/view/1402280124
The first link- where add is- it is working fine, but the inner join is not working. The SQL is correct, just not the code in the controller. Another issue I am having is the undefined action views. I am certain it has to do with what's inside the controller.
<?php
class ClientsController extends AppController
{
var $name = 'Clients';
function index()
{
$this->set('Clients', $this->Client->find('all'));
}
function add()
{
if ($this->request->is('post')) {
if ($this->Client->save($this->request->data)) {
$this->Session->SetFlash('Client has been saved');
$this->redirect(array(
'action' => 'index'
));
} else {
$this->Session->setFlash('Nope');
}
}
}
public function view($id = null)
{
$this->Client->id = $id;
$this->set('Client', $this->Client->read(null, $id));
}
public function edit($id = null)
{
$this->Client->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Client->read();
} else {
if ($this->Client->save($this->request->data)) {
$this->Session->setFlash('Client has been updated.');
$this->redirect(array(
'action' => 'index'
));
} else {
$this->Session->setFlash('Unable to update Client.');
}
}
$this->loadModel('Employee');
$this->set('Employees', $this->Employee->find('list', array(
'order' => array(
'Employee.name'
)
)));
$this->set(compact('Employees'));
}
public function delete($id = null)
{
if ($this->request->is('/Clients/delete/{id}')) {
throw new MethodNotAllowedException();
}
if ($this->Client->delete($id)) {
$this->Session->setFlash('The Client with id: ' . $id . ' has been deleted.');
$this->redirect(array(
'action' => 'index'
));
}
}
}
?>
Related
if I click on login button, it is taking empty input values since the condition is directly going into else part of cakephp.
Below is the code:
if($this->request->is('post'))
{
if(isset($this->data['Loginsubmit']))
{
if($this->data['Reg']['email']=='')
{
echo 'hii';
}
elseif( $this->data['Reg']['password']=='')
{
echo "hi";
}
else
{
$result = $this->Reg->find('list',array('conditions'=>array('email'=>$this->request->data['Reg']['email'], 'password'=>$this->request->data['Reg']['password'],'status'=>1)));
pr($result);
if(!empty($result)){
$email=$this->request->data['Reg']['email'];
$this->Session->write('Reg', $result);
$this->redirect(array('action' => 'login'));}
else{$this->Flash->error("invalid");
}
$result = $this->Reg->find('all');
$this->set('results',$result);
}
Hi please do like this:
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->fields = array(
'email' => 'email',
'password' => 'secretword'
);
}
public function login() {
if(!$this->Auth->Reg('id')){
$this->layout="login";
if ($this->request->is('post')) {
App::uses('Validation', 'Utility');
$user=0;
if(Validation::email($this->request->data['email']))
$user = $this->Reg->find('first', array(
'conditions' => array( 'Reg.email' => $this->request->data['email'],
),'recursive' => -1 ));
if($user)
{
$this->request->data['email']=$user['Reg']['email'];
}
$this->request->data=array('Reg'=>$this->request->data);
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
else
{
return $this->redirect($this->Auth->redirectUrl());
}
}
Please review & share your feedback.
i have 2 table in database.there are categories and posts.
categories have id , name
posts have id , category
and i have 2 files.
PostsController.php
edit.ctp
i edit category by edit.ctp when i save its
image >> http://s704.photobucket.com/albums/ww41/018115496/edit.png
It is not save category's name but save a category's id into Posts.
image >> http://i704.photobucket.com/albums/ww41/018115496/index.png
This is my code
PostsController.php
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public $uses = array('Post','Category');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function edit($id = null) {
$this->loadModel('Category');
$categories = $this->Category->find("list",array('field'=>array('Category.name')));
$this->set("categories", $categories);
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Post->id = $id;
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
}
and edit.ctp
<?php
echo $this->Form->create('Post');
echo $this->Form->input('category', array('options' => $categories));
echo $this->Form->end('Save Post');
?>
i think i because find("list") but i don't know how to solve.
Thank you.
I have fixed your problem, You need to replace this
//PostsController.php
public function edit($id = null) {
$this->loadModel('Category');
$categories = $this->Category->find("list",array('field'=>array('Category.name','Category.name')));
$this->set("categories", $categories);
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Post->id = $id;
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
When calling find('list'), the fields passed are used to determine what should be used as the array key and value, and optionally what to group the results by. By default, the primary key for the model is used for the key, and the display field (which can be configured using the model attribute displayField) is used for the value. Some further examples to clarify:
$justCategories = $this->Category->find('list', array(
'fields' => array('Category.name')
));
$Categories = $this->Category->find('list', array(
'fields' => array('Category.name', 'Category.name')
));
With the above code example, the resultant vars would look something like this:
$justCategories = Array
(
//[id] => 'name',
[1] => 'Books',
[2] => 'Baby',
[3] => 'Business & Industrial'
)
$Categories = Array
(
//[name] => 'name',
['Books'] => 'Books',
['Baby'] => 'Baby',
['Business & Industrial'] => 'Business & Industrial'
)
Read find(‘list’) in CakePHP
I follow a tutorial to add Full Calendar to my application . With the controller EventTypes, the index and the add view works , i've no problem .
For the view and edit view i get an error :
An Internal Error Has Occurred
Error: An Internal Error Has Occurred.
I'm using Cakephp 2 and PHP 5.4 .
This my controller :
<?php
class EventTypesController extends AppController {
var $name = 'EventTypes';
var $paginate = array(
'limit' => 15
);
public function admin_index() {
$this->EventType->recursive = 0;
$this->set('eventTypes', $this->paginate());
}
public function admin_view($id = null) {
if(!$id) {
$this->Session->setFlash(__('Invalid event type', true));
$this->redirect(array('action' => 'index'));
}
$this->set('eventType', $this->EventType->read(null, $id));
}
public function admin_add() {
if (!empty($this->data)) {
$this->EventType->create();
if ($this->EventType->save($this->data)) {
$this->Session->setFlash(__('The event type has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The event type could not be saved. Please, try again.', true));
}
}
}
public function admin_edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid event type', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->EventType->save($this->data)) {
$this->Session->setFlash(__('The event type has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The event type could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->EventType->read(null, $id);
}
}
public function admin_delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for event type', true));
$this->redirect(array('action'=>'index'));
}
if ($this->EventType->delete($id)) {
$this->Session->setFlash(__('Event type deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Event type was not deleted', true));
$this->redirect(array('action' => 'index'));
}
}
?>
My model :
<?php
class EventType extends AppModel {
var $name = 'EventType';
var $displayField = 'name';
var $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
);
var $hasMany = array(
'Event' => array(
'className' => 'FullCalendar.Event',
'foreignKey' => 'event_type_id',
'dependent' => false,
)
);
}
?>
I have the following relationship:
Edition:
public $hasAndBelongsToMany = array(
'Band' => array(
'className' => 'Band',
'joinTable' => 'bands_editions',
'foreignKey' => 'edition_id',
'associationForeignKey' => 'band_id',
'unique' => true,
)
);
Band:
public $hasAndBelongsToMany = array(
'Edition' => array(
'className' => 'Edition',
'joinTable' => 'bands_editions',
'foreignKey' => 'band_id',
'associationForeignKey' => 'edition_id',
'unique' => true,
)
);
BandsController:
public function add()
{
if($this->request->is('post'))
{
$this->Band->create();
if($this->Band->save($this->request->data))
doSomething();
else
doSomethingElse();
}
}
public function edit($id = NULL)
{
$this->Band->id = $id;
if(!$this->Band->exists())
throw new NotFoundException(__('Not found'));
if($this->request->is('post') || $this->request->is('put'))
{
if($this->Band->save($this->request->data))
doSomething();
else
doSomethingElse();
} else {
$this->request->data = $this->Band->read(NULL, $id);
}
}
When I try to populate Bands with add(), everything goes well, but as soon as I call edit(), Cake stops with this error:
Fatal error: Call to a member function schema() on a non-object
Debugging, I found that the error is fired when the parser reaches the check about the type of the request.
Which is my error?
Try this , I didn't see if it works
public function edit($id = null){
if(!$id){
throw new NotFoundException(__("Invalid Post"));
}
$band = $this->Band->findById($id);
if (!$band) {
throw new NotFoundException(__('Invalid Post'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Band->id = $id;
if ($this->Band->save($this->request->data)) {
doSomething();
}
else {
doDomething();
}
}
if (!$this->request->data) {
$this->request->data = $Band;
}
}
I have Two Table ones and twos I have A Foreign Key in 'two' table two_one_id
I Want to insert data in both table at a time , means insert data within one form, so how to manage controller and model, can I make one model and one controller for this ? then How To Create The Model And Controller For This
Which Type Of Relation Should Have I Prefer For Both Table?
I Have Made Two Different Tabele One.php and Two.php
and I Have Made Two Controller OnesController.php and twosController.php
Can I Use Scaffold In Both Of Them And Using Scaffold Can I Insert Data In Two Table Within A Controller And Model, If It Is Possible Using Scaffolding , Then How Done It, Or In This Code I Have Tried Without Scaffolding , Manual Manage View */
/*One.php File
public $displayField = 'name';
public $hasOne = array(
'Two' => array(
'className' => 'Two',
'foreignKey' => 'two_one_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
//Two.php
<?php
App::uses('AppModel', 'Model');
class Two extends AppModel {
public $displayField = 'sname';
public $belongsTo = array(
'One' => array(
'className' => 'One',
'foreignKey' => 'two_one_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
//OnesController.php
<?php
App::uses('AppController', 'Controller');
/**
* Ones Controller
*
* #property One $One
* #property PaginatorComponent $Paginator
*/
class OnesController extends AppController {
/**
* Helpers
*
* #var array
*/
public $helpers = array('Html','Form');
public $uses = array('One','Two');
public $components = array('Paginator');
public function index() {
$this->One->recursive = 0;
$this->set('ones', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->One->exists($id)) {
throw new NotFoundException(__('Invalid one'));
}
$options = array('conditions' => array('One.' . $this->One->primaryKey => $id));
$this->set('one', $this->One->find('first', $options));
}
public function add() {
if ($this->request->is('post')) {
$this->One->create();
if ($this->One->save($this->request->data)) {
$this->Session->setFlash(__('The one has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The one could not be saved. Please, try again.'));
}
}
}
public function edit($id = null) {
if (!$this->One->exists($id)) {
throw new NotFoundException(__('Invalid one'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->One->save($this->request->data)) {
$this->Session->setFlash(__('The one has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The one could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('One.' . $this->One->primaryKey => $id));
$this->request->data = $this->One->find('first', $options);
}
}
public function delete($id = null) {
$this->One->id = $id;
if (!$this->One->exists()) {
throw new NotFoundException(__('Invalid one'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->One->delete()) {
$this->Session->setFlash(__('The one has been deleted.'));
} else {
$this->Session->setFlash(__('The one could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}}
You may want to look into: $this->One->saveAll($this->request->data)
http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array
You Just Set Following Code to Your Controller
OnesControllers.php
public function add() {
if ($this->request->is('post')) {
$this->One->create();
}
if (!empty($this->request->data)) {
// We can save the User data:
// it should be in $this->request->data['User']
$one = $this->One->save($this->request->data);
// If the user was saved, Now we add this information to the data
// and save the Profile.
if (!empty($one)) {
// The ID of the newly created user has been set
// as $this->User->id.
$this->request->data['Two']['two_one_id'] = $this->One->id;
// Because our User hasOne Profile, we can access
// the Profile model through the User model:
$this->One->Two->save($this->request->data);
}
}
And Set This in Your View
view/one/add.ctp
<div class="ones form">
<?php echo $this->Form->create('One'); ?>
<fieldset>
<legend><?php echo __('Add One'); ?></legend>
<?php
echo $this->Form->input('name',array('rows' => '10'));
echo $this->Form->create('Two');
echo $this->Form->input('sname');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>