Php 如何从Yii2中的内部连接请求获取数据?
我正在尝试从此查询中获取数据:Php 如何从Yii2中的内部连接请求获取数据?,php,mysql,yii2,Php,Mysql,Yii2,我正在尝试从此查询中获取数据: SELECT threads.thread_id , threads.thread_name , threads.author_id , users.name FROM threads JOIN users ON threads.author_id = users.id ORDER BY thread_id LIMIT 5 它在phpmyadmin中工作得很好,但是当我使用Yi2使用以下代码进行尝试时
SELECT threads.thread_id
, threads.thread_name
, threads.author_id
, users.name
FROM threads
JOIN users
ON threads.author_id = users.id
ORDER
BY thread_id
LIMIT 5
它在phpmyadmin中工作得很好,但是当我使用Yi2使用以下代码进行尝试时:
$query = Threads::find();
$pagination = new Pagination([
'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);
$threads = $query->select(['threads.thread_id','threads.thread_name','threads.author_id','users.name'])
->from('threads')
->join('INNER JOIN', 'users', 'threads.author_id = users.id')
->orderBy('thread_id')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
然后在访问$thread->name的视图中,它给了我一个错误,说没有这样的字段,而“threads”表中的每个字段都可以正常工作
<?php foreach ($threads as $thread): ?>
<li>
<?= var_dump($thread)?>
<a href=" <?= Url::to(['thread/view', 'id' => $thread->thread_id])?>"><?=$thread->thread_name?></a>
<?= Html::encode("{$thread->thread_id} ({$thread->thread_name})") ?>:
<p>Author: <?=$thread->name?></p>
</li>
更新:var_dump$thread
object(app\models\Threads)#82 (13) { ["thread_id":"app\models\Threads":private]=> NULL ["thread_name":"app\models\Threads":private]=> NULL ["author_id":"app\models\Threads":private]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(3) { ["thread_id"]=> string(1) "1" ["thread_name"]=> string(30) "Привет, как дела?" ["author_id"]=> int(1) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(3) { ["thread_id"]=> string(1) "1" ["thread_name"]=> string(30) "Привет, как дела?" ["author_id"]=> int(1) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { }
它可能有用
$query = new Query();
$query->select([
'c.id as car_id',
'c.name_ru as car_name',
'c.slug_ru as car_slug',
'cp.price as price',
'cp.sztype_id as sztype_id',
])->from(['c'=>'c_car'])
->leftJoin(['cp'=>'c_car_price'],'cp.car_id=c.id')
->where(['c.collection_id'=>$collections[0]['collection_id'],'cp.show_price'=>self::ACTIVE_ONE])
->limit(6)
->all();
更新您的问题并显示准确的错误消息更新您的问题并显示var_dump$thread的结果您可以尝试将users.id添加到查询$query->select['threads.thread\u id'、'threads.thread\u name'、'threads.author\u id'、'users.name'、'users.id']您不需要->from'threads'只需删除它,线程将被自动选择。您正在使用线程模型进行查询,而不是新建\yii\db\query。哪一行在views\thread\index.php:14?@rob006其作者:此答案与问题完全无关。
$query = new Query();
$query->select([
'c.id as car_id',
'c.name_ru as car_name',
'c.slug_ru as car_slug',
'cp.price as price',
'cp.sztype_id as sztype_id',
])->from(['c'=>'c_car'])
->leftJoin(['cp'=>'c_car_price'],'cp.car_id=c.id')
->where(['c.collection_id'=>$collections[0]['collection_id'],'cp.show_price'=>self::ACTIVE_ONE])
->limit(6)
->all();