query finding wrong child items


I have an orders and items table setup as one to many.

When I execute the code, the orders info is correct, but the items found is wrong.

I checked the records and have the correct foreign key.

Here is my code.

$order = Orders::model()->findByPk($insert_id);

// I also tried

$order = Orders::model()->with('items')->findByPk($insert_id);

$message = "Order Number: " . $order->id . "<br/><br/>";

foreach($order->items as $item)


  $message .= "SKU: " . $item->item . "<br/>"; // wrong item



'items' => array(self::HAS_MANY, 'Items', 'id'),


'orders' => array(self::BELONGS_TO, 'Orders', 'order_id'),	

I just want to read the parent and get all items ordered.

Thanks for the help

Always use the foreign key column name on both sides of relationship definition:

'items' => array(self::HAS_MANY, 'Items', 'order_id'),

'orders' => array(self::BELONGS_TO, 'Orders', 'order_id'),

Thank You…

I did not know that.