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

How to insert multiple times the value of an attribute in single table in yii2
yii2 SluggableBehavior is applied to only emty slug field
Yii2: Create path and url for folder inside a module
Disable ActiveForm Clientside Validation on button other than submit Yii2
CRUD view link does not work in Yii2
Yii2 DynamicForm: Demo 3: Nested Dynamic Form with types of rooms
Company Follow Button disappear after login popup - Linkedin issue
How do I get rid of composer-asset-plugin in yii2
Yii2 Can not Store huge integer
Yii2 flash messages rendered automatically
Foreign Keys in yii2.0
ActiveForm doesn't submitting after clicking submit Yii2
Does Yii2 AR class can check, does two objects has a link?
Yii2 dependent DropdownList State and City
Add more action in yii2 grid-actionColumn
yii2 “urlmanager” rules “conflict” issue?

Categories

HOME
twitter
google-chrome-extension
pdf
client
c#-4.0
google-api-php-client
comparison
objectgears
sd-card
bookshelf.js
google-docs
getelementsbytagname
infragistics
youtube-api-v3
frameworks
binary-tree
enterprise-library-5
add
facebook-php-sdk
spring-kafka
quickbooks
postgres-xl
correlation
spring-tool-suite
alignment
n-gram
decimal
seaborn
angular-ui
reactcsstransitiongroup
google-pagespeed
paging
functional-testing
firebase-crash-reporting
phpfox
kendo-datasource
frame
airconsole
scorm2004
webtest
azure-sql-database
atl
force-layout
bytecode-manipulation
host
swisscomdev
elmah
android-mediaprojection
vao
gpx
parentheses
yii2-extension
lowpass-filter
nativeapplication
dynamics-sl
dwscript
orthogonal
background-service
medium.com
jquery-filter
setuptools
radtreelist
multi-level
merge-conflict-resolution
lttng
skype4py
essence
whois
hexdump
tarjans-algorithm
cakephp-3.1
dereference
formatjs
xceed-datagrid
undefined-reference
quantlib-swig
cdc
blending
tws
eclipse-memory-analyzer
mqx
html-editor
jquery-knob
isnullorempty
hosts-file
php-parser
external-accessory
mod-auth
genshi
oncheckedchanged
google-friend-connect
asp.net-mvc-areas
infrastructure
nhibernate.search
dbal
compiler-specific
ugc
commodore
windows-live-messenger

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