Note that there are some explanatory texts on larger screens.

plurals
  1. POcakephp insert data in two table within one model and control
    primarykey
    data
    text
    <p>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?</p> <p>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 */</p> <p>/*One.php File <pre><code> public $displayField = 'name'; public $hasOne = array( 'Two' =&gt; array( 'className' =&gt; 'Two', 'foreignKey' =&gt; 'two_one_id', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '' ) ); } </code></pre> <p>//Two.php</p> <pre><code>&lt;?php App::uses('AppModel', 'Model'); class Two extends AppModel { public $displayField = 'sname'; public $belongsTo = array( 'One' =&gt; array( 'className' =&gt; 'One', 'foreignKey' =&gt; 'two_one_id', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '' ) ); } </code></pre> <p>//OnesController.php</p> <pre><code> &lt;?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-&gt;One-&gt;recursive = 0; $this-&gt;set('ones', $this-&gt;Paginator-&gt;paginate()); } public function view($id = null) { if (!$this-&gt;One-&gt;exists($id)) { throw new NotFoundException(__('Invalid one')); } $options = array('conditions' =&gt; array('One.' . $this-&gt;One-&gt;primaryKey =&gt; $id)); $this-&gt;set('one', $this-&gt;One-&gt;find('first', $options)); } public function add() { if ($this-&gt;request-&gt;is('post')) { $this-&gt;One-&gt;create(); if ($this-&gt;One-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The one has been saved.')); return $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The one could not be saved. Please, try again.')); } } } public function edit($id = null) { if (!$this-&gt;One-&gt;exists($id)) { throw new NotFoundException(__('Invalid one')); } if ($this-&gt;request-&gt;is(array('post', 'put'))) { if ($this-&gt;One-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The one has been saved.')); return $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The one could not be saved. Please, try again.')); } } else { $options = array('conditions' =&gt; array('One.' . $this-&gt;One-&gt;primaryKey =&gt; $id)); $this-&gt;request-&gt;data = $this-&gt;One-&gt;find('first', $options); } } public function delete($id = null) { $this-&gt;One-&gt;id = $id; if (!$this-&gt;One-&gt;exists()) { throw new NotFoundException(__('Invalid one')); } $this-&gt;request-&gt;onlyAllow('post', 'delete'); if ($this-&gt;One-&gt;delete()) { $this-&gt;Session-&gt;setFlash(__('The one has been deleted.')); } else { $this-&gt;Session-&gt;setFlash(__('The one could not be deleted. Please, try again.')); } return $this-&gt;redirect(array('action' =&gt; 'index')); }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload