我有2个模特
class Product extends Eloquent {
protected $table = 'products';
public function product_data() {
return $this->hasMany('Product_translate','id_product');
}
}
和
class Product_translate extends Eloquent {
protected $table = 'product_translates';
public function product() {
return $this->belongsTo('Product','id_product');
}
}
在我的控制器中,如果我尝试使用Eloquent:
class PublicController extends BaseController {
public function home()
{
$products = Product::find(1)->product_data;
$queries = DB::getQueryLog();
$last_query = end($queries);
echo "<pre>";
print_r($last_query);
print_r($products);
echo "</pre>";
}
}
这是我获得的:
Array
(
[query] => select * from `tp450products` where `id` = ? limit 1
[bindings] => Array
(
[0] => 1
)
[time] => 0.88
)
没有结果,只能看到查询(在phpmyadmin中正确运行),
但是,如果我尝试仅使用find(1)来使用更改:
class PublicController extends BaseController {
public function home()
{
$products = Product::find(1);
$queries = DB::getQueryLog();
$last_query = end($queries);
echo "<pre>";
print_r($last_query);
print_r($products);
echo "</pre>";
}
}
我获取了表“产品”的记录,但我希望通过“product_translates”获得联接。 不知道出了什么问题!
数据库已正确链接,“product_translates”具有正确的外键
这里有2个表(只记录每个表):
Products
id | type | best | featured | voto | created_at | updated_at
1 | 1 | 0 | 0 | 0 | 2014-03-19 00:00:00 | 2014-03-12 13:26:23
Product_translates
id | id_product | id_lang | nome | slug
1 | 1 | 1 | mutande ascellari | mutande-ascellari
编辑1
使用查询构建器没问题,但为什么使用Eloquent ORM我遇到了这个问题?
class PublicController扩展了BaseController {
public function home()
{
$products = DB::table('products')->leftJoin('product_translates','product_translates.id_product','=','products.id')->get();
$queries = DB::getQueryLog();
$last_query = end($queries);
echo "<pre>";
print_r($last_query);
print_r($products);
echo "</pre>";
}
}
答案 0 :(得分:0)
尝试执行此操作,让我们看看你得到了什么:
class PublicController extends BaseController {
public function home()
{
$product = Product::find(1);
$p1 = Product::with('product_data')->where('id',1)->get();
echo "<pre>";
echo '---- Product';
var_dump($product);
echo '---- Product data values';
var_dump($product->product_data);
echo '---- Product data query';
var_dump($product->product_data());
echo '---- Product data eager loaded';
var_dump($p1);
echo "</pre>";
}
}
答案 1 :(得分:0)
这个问题长时间杀了我..这是一个真正的解决方案
Product_translate的型号名称应为ProductTranslate,然后您不需要设置表名属性...在指定的类名称和(多名称部分)中,命名非常重要。解决方案如下:
//file models/Product.php
class Product extends Eloquent
// in this name glass you well get $product->product_translates property
// gift from eloquent dynamic property!!
public function productTranslates() {
return $this->hasMany('ProductTranslate');
}
}
//file models/Product.php
class ProductTranslate extends Eloquent {
public function product() {
return $this->belongsTo('Product');
}
}
// this will be work now!!
class PublicController extends BaseController {
public function home()
{
$products = Product::find(1);
foreach($products->product_translates as $product_translate)
echo $product_translate->name . " <br> ";
$translates = ProductTranslate::All();
foreach($translates as $translate)
echo $translate->name. ": ". $translate->product->price . " <br> ";
}
}