This is my one of the controller
app\Controller\DashboardsController.php
here Patientslist is my another controller
<?php
class DashboardsController extends AppController {
public $components = array('Session');
public function index() {
$this-> loadModel('Patientslist');
$this->set('posts', $this->Patientslist->find('all', array('conditions' => array('Patientslist.user_id' => $this->Auth->user('id')))));
}
public function view($id) {
$this-> loadModel('Patientslist');
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Patientslist->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
$this-> loadModel('Patientslist');
if ($this->request->is('post')) {
//Added this line
$this->request->data['Patientslist']['user_id'] = $this->Auth->user('id');
if ($this->Patientslist->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
}
}
/* public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->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.'));
}
}
*/
public function edit($id = null) {
$this-> loadModel('Patientslist');
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Patientslist->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('Patientslist', 'put'))) {
$this->Patientslist->id = $id;
if ($this->Patientslist->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;
}
}
public function delete($id) {
$this-> loadModel('Patientslist');
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Patientslist->delete($id)) {
$this->Session->setFlash(
__('The post with id: %s has been deleted.', h($id))
);
return $this->redirect(array('action' => 'index'));
}
}
public function isAuthorized($user) {
$this-> loadModel('Patientslist');
// All registered users can add posts
if ($this->action === 'add') {
return true;
}
// The owner of a post can edit and delete it
if (in_array($this->action, array('edit', 'delete'))) {
$postId = $this->request->params['pass'][0];
if ($this->Patientslist->isOwnedBy($postId, $user['id'])) {
return true;
}
}//
return parent::isAuthorized($user);
}
}
here view app\View\Dashboards\index.ctp
<div id="tabs">
<ul>
<li>MyProfile</li>
<li>Patients</li>
<li>List</li>
</ul>
<div id="tabs-1">
</div>
<div id="tabs-2">
<p><?php echo $this->Html->link('Add Patient', array('action' => 'add')); ?></p>
<table>
<tr>
<th>Patient's Name</th>
<th>Address</th>
<th>Email-id</th>
<th>Mobile</th>
<th>Age</th>
<th>gender</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php
echo $this->Html->link(
$post['Patientslist']['patients_name'],
array('action' => 'view', $post['Patientslist']['id'])
); ?></td>
<td>
<?php
echo $post['Patientslist']['address'];
?>
</td>
<td>
<?php
echo $post['Patientslist']['email'];
?>
</td>
<td>
<?php
echo $post['Patientslist']['mobile'];
?>
</td>
<td>
<?php
echo $post['Patientslist']['age'];
?>
</td>
<td>
<?php
echo $post['Patientslist']['gender'];
?>
</td>
<td>
<?php
echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Patientslist']['id']),
array('confirm' => 'Are you sure?')
);
?>
<?php
echo $this->Html->link(
'Edit', array('action' => 'edit', $post['Patientslist']['id'])
);
?>
</td>
<td>
<?php echo $post['Patientslist']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div id="tabs-3">
</div>
</div>
Here I have to do display same tab for all add, edit, delete in same page how it will be possible?
Related
I created an application form using cakephp 2.
Now I want to know, How can users view only their application details using their user id. Here is the Form controller, the application form and the table for displaying the form
//Form controller
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Flash->success(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Unable to add your post.'));
}
}
//Create form
echo $this->Form->create('Post');
echo $this->Form->input('esta',['label'=>'New or Estabilished']);
echo $this->Form->end('Save Post');
//Form display
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['describe_idea'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
You said that you are using cakephp 2.x please find below code to find record
for Single Record
$posts = $this->Post->find('first', array(
'conditions' => array('id' => 1)
));
For Multiple record
$posts = $this->Post->find('all', array(
'conditions' => array('id' => 1)
));
Add filter in your action
CakePHP 3.x
$posts = $this->Posts->find()
->where([
'Posts.user_id' => $this->Auth->user('id')
]);
CakePHP 2.x
$posts = $this->Posts->find('all', [
'user_id' => $this->Auth->user('id')
]);
Note: Make sure to login user to set Auth data.
I am trying to create a car rental site, I lived a problem when the client reserve a car, the car must be removed from the window of available cars after reservation, I have a table 'cars 'and table' locations', what I want is when the client rent a car, the car must be removed from the list of cars for rent, here are the pages : /view/cars/index.ctp, /view/cars/view.ctp, /LocationsController.php, CarsController.php
view/cars/index.ctp :
<div class="row">
<?php foreach ($cars as $car):?>
<div class="col-sm-6 col-md-4">
<div class="">
<?php echo $this->Html->link($this->Html->image($car['Car']['avatar']),
array('action'=>'view',$car['Car']['id']),
array('escape'=>false,'class'=>'thumbnail'));?>
<div class="caption">
<h5>
<?php echo $car['Car']['title'];?>
</h5>
<h5>
Price: $
<?php echo $car['Car']['price'];?>
</h5>
<h5><?php echo $this->Html->link(__('Rent a Car'), array('controller'=>'cars','action' => 'view', $car['Car']['id'])); ?></h5>
</div>
</div>
</div>
<?php endforeach;?>
and the view/cars.view.ctp :
<div class="cars view">
<h2><?php echo __('Car'); ?></h2>
<dl>
<dt><?php echo __('Id'); ?></dt>
<dd>
<?php echo h($car['Car']['id']); ?>
</dd>
<dt><?php echo __('Picture'); ?></dt>
<dd>
<?php echo h($car['Car']['picture']); ?>
</dd>
<dt><?php echo __('Price'); ?></dt>
<dd>
<?php echo h($car['Car']['price']); ?>
</dd>
</dl>
</div>
<?php
echo $this->Html->image("cars/".$car['Car']['id'].".jpg", array(
"alt" => "Cars",
'url' => array('controller' => 'locations', 'action' => 'add', $car['Car']['id'])
));
?>
//and the CarsController :
<?php
App::uses('AppController', 'Controller');
class CarsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array('Js', 'GoogleMap');
public function view($id = null){
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function index() {
$this->set('cars', $this->Car->find('all'));
}
}
//and the LocationsController
<?php
App::uses('AppController', 'Controller');
class LocationsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array(
'Js',
'GoogleMap'
);
public function index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function add($car_id) {
if ($this->request->is('post')) {
$this->Location->create();
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
/*$cars = $this->Location->Car->find('list');*/
$this->set(compact('agencies'));
$this->set('car_id', $car_id);
}
public function edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
}
I've done blog by tutorial and stopped at Deleting Posts (all code is by this link), because has Notice (1024): Element Not Found: Elements\8.ctp ( 8 - id of deleted record) when click Delete in table on index page. And record is really deleted in database. How to solve this notice? My version cakephp is 2.6.1.
Thanks.
<?php
class PostsController extends AppController {
public $helpers = array ('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('posts',$this->Post->find('all'));
}
public function view($id = null)
{
if (!$id)
{
throw new NotFoundException(__('Invalid Post'));
}
$post=$this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post',$post);
}
public function add()
{
if ($this->request->is('post'))
{
$this->Post->create();
if ($this->Post->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'));
}
}
public function edit($id = null)
{
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;
}
}
public function delete($id)
{
if ($this->request->is('get'))
{
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id))
{
$this->Session->setFlash(__('The post with id: %s has been deleted'),h($id));
return $this->redirect(array('action'=>'index'));
}
return $this->redirect(array('action'=>'index'));
}
}
// index.ctp
<h1>Blog posts</h1>
<?php
echo $this->Html->link(
'Add Post',array('controller'=>'posts','action'=>'add')
);
?>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id'];?></td>
<td>
<?php
echo $this->Html->link($post['Post']['title'],array(
'controller' => 'posts',
'action' => 'view',
$post['Post']['id']
));
?>
</td>
<td>
<?php
echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Post']['id']),
array('confirm' => 'Are you sure?')
);
?>
<?php
echo $this->Html->link(
'Edit',
array('action' => 'edit', $post['Post']['id'])
);
?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
$this->Session->setFlash(__('The post with id: %s has been deleted'),h($id));
the problem is here,
setFlash(__('The post with id: %s has been deleted'),h($id));
this should be,
setFlash(__('The post with id: %s has been deleted',h($id)));
Because you didn't provide code (not smart if you want to get help), I guess you're calling somewhere $this->element($id); in your view. The tutorial you've linked doesn't contain this method call anywhere. So you do something different.
Also read the error message, it tells you that it it missing a file called 8.ctp in your app/View/Elements/ folder. To figure out why is now up to you.
Cake php blog example with auth login pages here index() method i have to disply his own post other post should not be display .. here i changed but it shows error so changed to basic can any one help me
controller/PatientslistController.php
<?php
class PatientslistController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('posts', $this->Patientslist->find('all'));
}
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Patientslist->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if ($this->request->is('post')) {
//Added this line
$this->request->data['Patientslist']['user_id'] = $this->Auth->user('id');
if ($this->Patientslist->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
}
}
/* public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->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.'));
}
}
*/
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Patientslist->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('Patientslist', 'put'))) {
$this->Patientslist->id = $id;
if ($this->Patientslist->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;
}
}
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Patientslist->delete($id)) {
$this->Session->setFlash(
__('The post with id: %s has been deleted.', h($id))
);
return $this->redirect(array('action' => 'index'));
}
}
public function isAuthorized($user) {
// All registered users can add posts
if ($this->action === 'add') {
return true;
}
// The owner of a post can edit and delete it
if (in_array($this->action, array('edit', 'delete'))) {
$postId = $this->request->params['pass'][0];
if ($this->Patientslist->isOwnedBy($postId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
}
?>
model/Patientslist.php
<?php class Patientslist extends AppModel {
public function isOwnedBy($post, $user) {
return $this->field('id', array('id' => $post, 'user_id' => $user)) === $post;
}
}
?>
view/Patientslist/index.ctp
<h1>Blog posts</h1>
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Patientslist']['id']; ?></td>
<td>
<?php
echo $this->Html->link(
$post['Patientslist']['title'],
array('action' => 'view', $post['Patientslist']['id'])
);
?>
</td>
<td>
<?php
echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Patientslist']['id']),
array('confirm' => 'Are you sure?')
);
?>
<?php
echo $this->Html->link(
'Edit', array('action' => 'edit', $post['Patientslist']['id'])
);
?>
</td>
<td>
<?php echo $post['Patientslist']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
public function index() {
$this->set('posts', $this->Patientslist->find('all', array(
'conditions' => array(
'Patientslist.user_id' => $this->Auth->user('id')));
}
Change "user_id" with related user field in PatientLists table.
hi all when trying to update my database, this function isn't grabbing the id from the table and it isn't updating that line in the table. Its also throwing an error with
$this->Relationship->id = $this->request->data['id'];
here is the function in its entirety
public function approve($id=null){
$this->Relationship->id = $id;
if($this->request->is('get')){
$this->request->data=$this->Relationship->read();}
$this->Relationship->id = $this->request->data['id'];
if($this->Relationship->save($this->request->data)) {
$this->Session->setFlash('Your Relationship has been updated.');
$this->redirect(array('action' => 'request'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
here is form/view
<?php
echo $this->Form->create('Relationship', array('action'=>'approve'));
echo $this->Form->input('expirydate',array('label'=>'Expiry Date: ', 'class' => 'dateclass'));
echo $this->Form->end('Submit');
?>
what I'm trying to do with this function is grab a the id, and edit two fields in that entry
It should be $this->request->data['Relationship']['id']; if you setup your form correctly. Also, you can just do
$this->Relationship->create($this->request->data);
$this->Relationship->save()
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';
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.');
}
}
}
I also had to change a tinyint from 0=>1 in the database.The other issue I had was with my request view, it was not passing the id to the approve function. Once I changed the code to this it worked
<?php foreach($Relationships as $relationship):?>
<tr>
<td align='center'><?php echo $relationship['Relationship']['partyone']; ?></td>
<td align='center'><?php echo $relationship['Relationship']['partytwo']; ?></td>
<td> </td>
<td><?php echo $this->Html->link($relationship['Relationship']['partyone'], array('action'=>'approve', $relationship['Relationship']['id'])); ;?>
</td>
</tr>
<?php endforeach; ?>
</table>