yii2


Yii2 Select only few columns from related model


In controller i have:
public function actionGetItems()
{
$model = new \app\models\WarehouseItems;
$items = $model->find()->with(['user'])->asArray()->all();
return $items;
}
In WarehouseItem model i have standard (created by gii) relation declaration:
public function getUser()
{
return $this->hasOne('\dektrium\user\models\User', ['user_id' => 'user_id']);
}
How can i control which column data do i get from "user" relation? I currently get all columns which is not good as that data is being sent to Angular in JSON format.
Right now i have to loop trough $items and filer out all columns i dont want to send.
You should simply modify the relation query like this :
$items = \app\models\WarehouseItems::find()->with([
'user' => function ($query) {
$query->select('col1, col2');
}
])->asArray()->all();
Read more : http://www.yiiframework.com/doc-2.0/yii-db-activequerytrait.html#with()-detail

Related Links

Capcha always show incorrect
Integration Reportico in Yii2 Projects
Yii2 : Subdirectory in yii2 not working
Yii2 KCFinder with CKEditor
Yii2 - validation compare acting strange
Updating composer throws exception Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
Dynamic declaration of a model in Yii2
image are save in folder but not saved in database in yii2.0
how can i put sidebar menu in right side using yii2
Yii2 afterSave. Using changedAttributes show error
how to do a nested foreach loop in yii2?
Yii2, Set multiple Value Select2
update single field directly gave value in update function yii2
How can i use multi controller in one page?
PHP Composer “require” OR condition with version in future
Yii2 DropdownList on relationated tables

Categories

HOME
java
vmware
yahoo-oauth
cakephp-2.5
v8
disassembler
mvvmcross
networkx
portia
node-notifier
wheelnav.js
leiningen
foselasticabundle
oxyplot
errorlevel
graphicsmagick
filezilla
spring-mybatis
replaceall
ejabberd-module
wtx
google-cloud-nl
srcset
preg-match
gtrendsr
hexo
xcode-extension
retina-display
mixture-model
dynamics-crm-2013
impersonation
splice
cookiecutter-django
email-templates
ruby-on-rails-3.1
google-closure
http-live-streaming
reactive-cocoa-5
dandelion
fancybox-2
mu
setter
sqlclient
python-cryptography
cudafy.net
or-tools
specrun
deadbolt-2
google-feed-api
bonobo
sts-springsourcetoolsuite
url-masking
lttng
superstack
msys
cartesian-product
icu4j
citrus-pay
pundit
sframe
android-listview
ideamart
dundas
formatjs
winddk
mdt
openexr
wp-query
concurrent-collections
srs
typo3-neos
bulkloader
html4
wsdl-2.0
distutils
jquery-knob
genshi
oncheckedchanged
rescale
perfect-hash
microsoft-virtualization
yagni
aquaticprime
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App