title: "Undefined index" trying to add ORM object in Kohana php framework
body: <p>I'm trying to add an ORM object to another one (one to many relationship) in Kohana:</p>
  
  <pre><code>$item = $cart-&gt;cartitems-&gt;where('productid', '=', $product-&gt;id);
  
      //if($item == null)//This apparently doesn't work in Kohana, it returns an object, even if not loaded, using the following:
      if (!$item-&gt;loaded()) {
          $item = new Model_Cartitem();
          $item-&gt;productid = $product-&gt;id;
          $item-&gt;quantity = 1; //TODO: Change to incrementation
          $item-&gt;totalprice = $product-&gt;price; //TODO: Change to incrementation
          $item-&gt;unitprice = $product-&gt;price;
          $item-&gt;productid = $product-&gt;id;
          $item-&gt;productnumber = $product-&gt;productnumber;
          $cart-&gt;add('cartitem', $item);
      }
  
  
      $cart-&gt;save();
  </code></pre>
  
  <p>Here's the cart class:</p>
  
  <pre><code>class Model_Cart extends ORM
  {
      protected $_has_many = array('cartitems' =&gt; array());
  }
  </code></pre>
  
  <p>and the cartitem class:</p>
  
  <pre><code>class Model_Cartitem extends ORM {
      protected $_belongs_to = array('cart' =&gt; array('foreign_key' =&gt; 'cartid'));
  }
  </code></pre>
  
  <p>But when I run it I get the error "ErrorException [ 8 ]: Undefined index: cartitem ~ MODPATH\orm\classes\kohana\orm.php [ 1403 ]"</p>
  
  <p>What does this "undefined index" refer to, and how do I fix this?</p>
  
  <p>EDIT:</p>
  
  <p>Changed the belongs_to in Model_Cartitem. Here is the definition of my foreign key in table cartitems (from NaviCat):</p>
  
  <p><img src="https://i.stack.imgur.com/3MHnz.png" alt="Foreign key in cartitems table"></p>
  
  <p>EDIT 2:</p>
  
  <p>Here's the sql code for the tables:</p>
  
  <p>cartitems:</p>
  
  <pre><code>CREATE TABLE `cartitems` (
      `id` INT(11) NOT NULL AUTO_INCREMENT,
      `productid` INT(11) NOT NULL,
      `quantity` INT(11) NOT NULL,
      `totalprice` DOUBLE NOT NULL,
      `cart_id` INT(11) NOT NULL,
      `productname` TEXT NOT NULL,
      `unitprice` DOUBLE NOT NULL,
      `productnumber` TEXT NOT NULL,
      PRIMARY KEY (`id`),
      INDEX `cart_id` (`cart_id`),
      CONSTRAINT `cart_id` FOREIGN KEY (`cart_id`) REFERENCES `carts` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
  )
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  ROW_FORMAT=DEFAULT
  AUTO_INCREMENT=2
  </code></pre>
  
  <p>carts:</p>
  
  <pre><code>CREATE TABLE `carts` (
      `id` INT(11) NOT NULL AUTO_INCREMENT,
      `username` TEXT NOT NULL,
      PRIMARY KEY (`id`)
  )
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  ROW_FORMAT=DEFAULT
  AUTO_INCREMENT=2
  </code></pre>
  
  <p>And since I changed the foreign key id name to cart_id I would think I should be able to use the simpler relationship definitions in the Model classes, so I changed to this:</p>
  
  <pre><code>class Model_Cart extends ORM
  {
      protected $_has_many = array('cartitems' =&gt; array());
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Model_Cartitem extends ORM
  {
      protected $_belongs_to = array('cart' =&gt; array());
  }
  </code></pre>
  
  <p>Still doesn't work, still getting the undefined index: cartitem error...</p>
  
  <p>EDIT 3:</p>
  
  <p>Ok, so I tried doing the exact same thing in Asp.Net MVC 3, using the Entity Framework as ORM, just to see if there was something wrong with the database table definitions. But everything worked just fine there.</p>
  
  <p>So at least I know now it has nothing to do with the database being faulty. So the problem must lie in the Kohana ORM and I must be doing something wrong there. I tried the suggestions from Yoda, but nothing has helped so far. This is getting frustratingly difficult, maybe I should look at CodeIgniter instead, which is supposed to be simpler... But I've kind of liked Kohana in other ways. Is there really no one who knows what the problem with my ORM classes is?</p>
  
  <p>BTW: Is this part wrong? $cart->add('cartitem', $item); It's the only way I've found to add a relationship object, but at the same time, it says in the docs that you can add relationships in a many-to-many relationship this way... But if it doesn't apply to one-to-many, then how would I add the new cartitem to the cart?</p>