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

Include additional library to yii2
Yii2 make session works on all actions of one controller?
Yii2 display data from phpexcel in view before saving to database
yii2 dependant dropdown reset on edit action due to onchange function
Yii2 non-DB (or virtual) attribute isn't populated during massive assignment?
Yii2 Join multiple tables
Codeception Adding custom assert actions (functional testing)
Selected values in yii2
Yii queryBuilder; If dbexpression is not being quoted well
Time attribute validation in Yii2 framework
Yii2 kartik-datecontrol extension
SQL query help in Yii2
Yii2 Pjax inside ListView not working
how to get post value from multiple array in yii2
I want to display column data in Yii2 listview
Yii2 - How We can Change the element that Will be update via Pjax response

Categories

HOME
openlayers
hook
keras
gremlin
google-play
appx
electron
read-eval-print-loop
bookshelf.js
leon
electronics
blueprintjs
echarts
indesign
synchronization
virtualization
quicklook
serverless-framework
workload-scheduler
evopdf
connection-string
flux
autocad-plugin
object-detection
functional-testing
facebook-instant-articles
sylius
wkwebview
autosys
microsoft-sync-framework
underflow
uninstall
subdomains
google-rich-snippets
uisplitview
data-manipulation
create-table
google-qpx-express-api
winrt-xaml-toolkit
perlin-noise
rotational-matrices
amazon-kinesis-kpl
force-layout
ios5
xcode-extension
dynamics-crm-2013
streamsets
code-search-engine
ansible-playbook
trim
broadcastreceiver
ws-security
ncalc
ruby-on-rails-3.1
powershell-dsc
.net-4.6.2
logfiles
parentheses
jedis
boost-multi-index
svmlight
colorama
directory-structure
boost-preprocessor
nbconvert
spring-mongodb
account-kit
background-service
medium.com
cloudbees
phpcas
browser-link
endeca-workbench
tableau-online
sniffer
graph-api-explorer
pretty-print
facebook-graph-api-v2.4
cctv
xojo
sgen
datagridviewcolumn
hyprlinkr
ember-charts
ccss
jmapviewer
comaddin
dmoz
rdoc
coderush
errai
spring-portlet-mvc
gwt-ext
javax.script
mysql-error-1005
xfbml
paster
preference
nintendo-ds
audio-capture
gacutil

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