我想知道模型中的laravel 4关系如何工作。这个问题是因为我正在尝试从2个具有相关性的表中获取行,但到目前为止还没有成功。
class Products extends Eloquent {
protected $table = "Products";
protected $primaryKey = "ProductId";
public function plandetail()
{
return $this->belongsTo("PlanDetail", "PlanDetail.ProductId")->select( array("ProductId", "Order") );
}
public function getProductsPlan($dealerId)
{
return $this->with( "plandetail" )->where("Products.DealerId", $dealerId)->get();
}
} // end model
// In my controller
$product = new Products();
$products = $product->getProductsPlan($id);
foreach( $products as $product )
{
print_r($product); // prints only the rows from the Products table
// not the PlanDetail table.
}
在输出中仅打印产品记录,而不打印PlanDetail表行。我是否需要添加与PlanDetail表的联接才能获取这些行?
这是即时消息正在获取的示例行:
Products Object ( [table:protected] => Products [primaryKey:protected] => ProductId [errors:Products:private] => [rules:Products:private] => Array ( [ProductBaseId] => required|numeric [DealerId] => required|numeric [ProductName] => required|max:255 [DisplayName] => required|max:255 [Bullets] => required [Cost] => required|numeric [SellingPrice] => required|numeric [UseWebServicePricing] => required|boolean [Term] => required|numeric [Type] => required|numeric [Deductible] => required|numeric [VehiclePlan] => required|numeric [Mileage] => required|numeric [TireRotation] => required|numeric [Interval] => required|numeric [UseRangePricing] => required|boolean [IsTaxable] => required|boolean [NotRegulated] => required|boolean [CreatedOn] => required [CreatedBy] => required [ModifiedBy] => required [ModifiedOn] => required ) [connection:protected] => [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [ProductId] => 3 [ProductBaseId] => 84 [DealerId] => 50 [ProductName] => Vehicle Service Contract [DisplayName] => US Warranty Service Contract [ProductDescription] => [Bullets] => Warranty Proteccion for the covered components,Rental Car Coverage,Towling Coverage,, [Cost] => 0.0 [SellingPrice] => 0.0 [BrochureImage] => [PDFContrator] => [UseWebServicePricing] => 1 [UseManualPricing] => 0 [BrochureHeight] => 0 [BrochureWidth] => 0 [Term] => 36 [Type] => Platinum [Deductible] => 100 [VehiclePlan] => None [Mileage] => 36000 [TireRotation] => 0 [Interval] => 0 [PENProductId] => 0 [DMSProductId] => 16 [CreatedBy] => GREIVIN BRITTON [CreatedOn] => 2015-01-08 22:26:47.000 [UseRangePricing] => 0 [ModifiedBy] => GREIVIN BRITTON [ModifiedOn] => 2015-01-28 15:06:11.000 [IsTaxable] => 0 [NotRegulated] => 0 ) [original:protected] => Array ( [ProductId] => 3 [ProductBaseId] => 84 [DealerId] => 50 [ProductName] => Vehicle Service Contract [DisplayName] => US Warranty Service Contract [ProductDescription] => [Bullets] => Warranty Proteccion for the covered components,Rental Car Coverage,Towling Coverage,, [Cost] => 0.0 [SellingPrice] => 0.0 [BrochureImage] => [PDFContrator] => [UseWebServicePricing] => 1 [UseManualPricing] => 0 [BrochureHeight] => 0 [BrochureWidth] => 0 [Term] => 36 [Type] => Platinum [Deductible] => 100 [VehiclePlan] => None [Mileage] => 36000 [TireRotation] => 0 [Interval] => 0 [PENProductId] => 0 [DMSProductId] => 16 [CreatedBy] => GREIVIN BRITTON [CreatedOn] => 2015-01-08 22:26:47.000 [UseRangePricing] => 0 [ModifiedBy] => GREIVIN BRITTON [ModifiedOn] => 2015-01-28 15:06:11.000 [IsTaxable] => 0 [NotRegulated] => 0 ) [relations:protected] => Array ( [plandetail] => ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [softDelete:protected] => )
提前致谢 :)
看来这里有些错误。
该belongsTo
方法需要相关模型的名称,然后是Products
表上该模型的外键。看来您的外键实际上在相关模型上,从而使该关系成为ahasOne
或hasMany
。在这种情况下,由于您说您期望2行,所以我假设hasMany
。
此外,此参数的第二个参数仅需要列的名称。它已经可以猜测表的名称,因为您为其指定了相关的模型名称。
考虑到这一点,这应该是您需要的关系方法。
public function plandetails()
{
return $this->hasMany("PlanDetail", "ProductId")->select( array("ProductId", "Order") );
}
并且其他功能应进行一些修改。当您使用with
并且需要基于关系进行查询时,您应该传递一个数组,其中键是关系的名称,值是一个回调函数,该函数将允许您修改查询以获取相关项。
public function getProductsPlan($dealerId)
{
return $this->with(array("plandetail", function($q) use ($dealerId)
{
$q->where('DealerID', $dealerId);
}))->get();
}
现在,当您执行此操作时...
$product = new Products();
$products = $product->getProductsPlan($id);
这应该工作...
foreach( $products as $product ) {
foreach($product->plandetails as $plandetail) {
print_r($plandetail);
}
}
我还修改了相关的函数名称,因此您在使用时必须更新getProductsPlan
函数以反映该函数with
。使用时hasMany
,将函数名称设为复数会更有意义并且更易于阅读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句