Note that there are some explanatory texts on larger screens.

plurals
  1. POcakePhp and a foreign key : i cannot write the foreign key in the view
    primarykey
    data
    text
    <p>i've got a problem with a foreign key field : i created a <code>categories</code> table with <code>MySQLWorkbench</code>, which is linked by a <code>1:n</code> relation to another table : <code>Articles</code>. It seems to work, because in <code>Articles</code>, there is a <code>categories_id</code> "INT" field .</p> <p>I baked the whole project, everything works fine, except this foreign key : instead of an input that would receive the "number" i would write to specify the <code>categories_id</code> number, there is a kind of empty "select" (list) that contains nothing, so i cannot enter any number for the <code>categories_id</code> field in my database :</p> <p>here's the image : <img src="https://i.stack.imgur.com/To43F.png" alt="my categories input"></p> <p>If i try to "force" and add an "article" (so without the category), there is this error :</p> <blockquote> <p>Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>ecommerce</code>.<code>articles</code>, CONSTRAINT <code>fk_articles_categories</code> FOREIGN KEY (<code>categories_id</code>) REFERENCES <code>categories</code> (<code>id</code>) ON DELETE NO ACTION ON UPDATE NO ACTION) </p> </blockquote> <p>And here's my <strong><code>add.ctp</code></strong> file (it is written <code>input</code> for the <code>categories_id</code>, so i don't know what to change) :</p> <pre><code>&lt;div class="articles form"&gt; &lt;?php echo $this-&gt;Form-&gt;create('Article');?&gt; &lt;fieldset&gt; &lt;legend&gt;&lt;?php echo __('Add Article'); ?&gt;&lt;/legend&gt; &lt;?php echo $this-&gt;Form-&gt;input('nom'); echo $this-&gt;Form-&gt;input('prix'); echo $this-&gt;Form-&gt;input('categories_id'); ?&gt; </code></pre> <p>Thanks for your help</p> <hr> <p>EDIT: </p> <p>here's what i added :</p> <pre><code>class Article extends AppModel { var $belongsTo = array( 'Category' =&gt; array( 'className' =&gt; 'Category', 'foreignKey' =&gt; 'category_id' ) ); } </code></pre> <p>and :</p> <pre><code>class Category extends AppModel { var $hasMany = array( 'Article' =&gt; array( 'className' =&gt; 'Article', 'foreignKey' =&gt; 'category_id' ) ); } </code></pre> <p>and in the controller (<code>add()</code>) : </p> <pre><code>public function add() { //$this-&gt;set('categories',$this-&gt;Article-&gt;Category-&gt;find('all')); //$c = $this-&gt;Article-&gt;Category-&gt;find('all', array("recursive"=&gt;-1, 'fields'=&gt;array('id', 'nom'))); $c = $this-&gt;Article-&gt;Category-&gt;find('list'); $this-&gt;set('cat', $c); </code></pre> <p>but in my view, <code>pr($cat)</code> returns an empty Array...</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;input('nom'); echo $this-&gt;Form-&gt;input('prix'); echo $this-&gt;Form-&gt;input('category_id', array('option'=&gt;$cat)); pr($cat); ?&gt; </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.
    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