Hello,
I am having some problem with related tables. I have the following tables
(definitions below) ingredients and ingredient_types. Ingredients has a
foreign key relationship to ingredient_types. I have two models Ingredients
and IngredientTypes (definitions below).
My expectation is that when I do the following in the IngredientsController:
function indexAction()
{
$this->view->title = "Ingredient";
$ingredients = new Ingredients();
$this->view->ingredients = $ingredients->fetchAll();
}
That I would get all the ingredients and all their related data. Something
similar to running the SQL:
SELECT * FROM ingredients, ingredient_types
WHERE ingredients.ingredient_type_id = ingredient_types.id
But for some reason that I cannot determine I am only get data from the
Ingredients model. My assumption is that the framework will know about the
relationship due to the presence of the foreign key.
Failing that I figured maybe I need to write the fetch method myself. I
tried to write a custom method in my Ingredients model to pull the data but
when I tried to run it I got an error saying that select cannot join the
tables.
Any help or advice is appreciated.
MODEL DEFINITION
class IngredientTypes extends Zend_Db_Table_Abstract
{
protected $_name = 'ingredient_types';
}
class Ingredients extends Zend_Db_Table_Abstract
{
protected $_name = 'ingredients';
}
TABLE DEFINITION
CREATE TABLE `ingredient_types` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`ingredient_type` varchar(64) NOT NULL DEFAULT '',
`description` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;
CREATE TABLE `ingredients` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`ingredient` varchar(64) NOT NULL DEFAULT '',
`ingredient_type_id` int(10) unsigned zerofill NOT NULL,
`description` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_ingredient` (`ingredient`),
KEY `ingredient_type_id` (`ingredient_type_id`),
CONSTRAINT `ingredient_type_id` FOREIGN KEY (`ingredient_type_id`)
REFERENCES `ingredient_types` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1;
--
View this message in context:
http://www.nabble.com/My-model-does-not-appear-to-be-working.-tp21986647p21986647.html
Sent from the Zend Framework mailing list archive at Nabble.com.