symfony many-to-many relation loops properties instead of objects
I have a many-to-many relation with a linked table. See (simplified) schema below. Created according to the tutorial (http://www.symfony-project.org/doctrine/1_2/en/05-Data-Fixtures#chapter_05_many_to_many)
The schema imports/builds correct and phpmyadmin shows the foreign keys correct. I'm under the impression that afterwards in the 'locatie' module's indexSuccess template i can call:
foreach($locatie->getProducts() as $oProduct):
echo $oProduct->naam;
endforeach;
But that doesnt work, because $oProduct doesnt appear to be an object but a string representing each property in the product class. The foreach simply loops the properties of the first product instead of the product list. Anybody any advice?
Schema
Locatie:
connection: doctrine
tableName: locatie
columns:
locatie_id:
type: integer(4)
fixed: false
unsigned: true
primary: true
autoincrement: true
naam:
type: string(255)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
LocatieProduct:
connection: doctrine
tableName: locatie_product
columns:
locatie_product_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: true
locatie_id:
type: integer(4)
fixed: false
unsigned: true
primary: false
notnull: true
autoincrement: false
product_id:
type: integer(4)
fixed: false
unsigned: true
primary: false
notnull: true
autoincrement: false
relations:
Locatie:
local: locatie_id
foreign: locatie_id
foreignAlias: LocatieProducts
onDelete: CASCADE
Product:
local: product_id
foreign: product_id
foreignAlias: LocatiePro开发者_JAVA百科ducts
onDelete: CASCADE
Product:
connection: doctrine
tableName: product
columns:
product_id:
type: integer(4)
fixed: false
unsigned: true
primary: true
autoincrement: true
naam:
type: string(255)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
You don't have Products defined as a relation on Locatie. Change your schema to:
Locatie:
connection: doctrine
tableName: locatie #this isn't necssary, by the way
columns:
#etc
relations:
Products:
class: Product
type: many
refClass: LocatieProduct
local: locatie_id #the field on LocatieProduct that is an FK to the id of the current table (Locatie)
foreign: product_id #the field on LocatieProduct that is an FK to the id of the class (Product)
Also note that you do not need the field locatie_product_id on LocatieProduct. If you do want that table to have a single primary key, I would simply name it id.
Here's more from the Doctrine book.
Welcome to Stack Overflow, tomvo.
The ORM creates a model for your intermediate or "through" class, so you have an extra class named LocatieProduct you're not expecting. You would use it like this:
foreach($locatie->getLocatieProducts()->getProduct() as $oProduct):
echo $oProduct->naam;
endforeach;
The best way for learning how to access the related objects is to read the generated code in lib/model/doctrine/base/
.
I often add additional methods to the model for convenience. For example, in lib/model/doctrine/Locatie.class.php
you can add a function to work more like you expect:
public function getProducts() {
$a = array();
foreach ($this->getLocatieProducts()->getProduct() as $p) {
$a[] = $p;
}
return $a;
}
Symfony scape strategy wraps objets when rendering, you should try to get the raw value in order to do what you want. Here is an exmaple:
foreach($locatie->getRawValue()->getProducts() as $oProduct):
echo sfOutputEscaper::unescape($oProduct->naam);
endforeach;
Hope that helps solve your problem!
精彩评论