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

Yii2: Registering javascript when loading (nested) Gridview through renderAjax
Yii2 set active user in API
Yii2 full calendar AjaxEvents not working properly
white color Issue after create thumbnail image in yii2
Creating a one-to-many relationship in Yii2
Handle Image update in yii2
Header and Footer missing in modal popup
Select2 Krajee widget - using modal and getting the id of the value
sjaakp/yii2-illustrated-behavior Yii2 extension for image not working
change the navigation bar url from yii2 without redirecting
Yii 2 - Conditional Assets?
How to get value of dropdown instead of id yii2
How can I access private value from object
How to add valueless attribute to <form> tag using yii2 ActiveForm
Register a js file independently in Assets
Get user's email from twitter login in yii2

Categories

HOME
cakephp
client
smarty
raspberry-pi
framework7
amazon-ecs
c#-2.0
ebean
blueprintjs
wamp
directx
add
windows-azure-storage
jplayer
visual-studio-2005
event-handling
decomposition
uitypeeditor
dosgi
openrefine
apache-metamodel
textfield
predix
kryo
sox
kvc
extjs5
spark-jobserver
selectedindexchanged
url-scheme
info.plist
entitlements
windows-server-2000
file-rename
gsmcomm
nat
google-rich-snippets
google-cloud-endpoints-v2
data-manipulation
usbserial
form-data
bower-install
x11-forwarding
libvpx
c11
particles.js
acoustics
qcombobox
sas-jmp
wptoolkit
flickr-api
azure-application-gateway
filepicker
termination
qsslsocket
alphabet
winscp-net
memory-alignment
arrow-keys
dwscript
background-service
medium.com
radtreelist
slicknav
phishing
fody
nsfilemanager
gridview-sorting
qcustomplot
master-slave
make-install
freedesktop.org
javax.sound.midi
tcpserver
maven-tomcat-plugin
lib.web.mvc
cartesian-product
coveralls
census
angular-local-storage
sonarqube5.1.2
graph-api-explorer
inmobi
fscommand
dia
camanjs
google-style-guide
qcodo
issuu
dealloc
picturefill
hyprlinkr
algebraic-data-types
centos5
ccss
apc
dbconnection
ril
batterylevel
seed
wsdl-2.0
selected
libstdc++
pysimplesoap
hgsubversion
gnustep
zpt
rfc1123
inotifycollectionchanged
sql-server-profiler
sproutcore-2
rescale
meego
uimenucontroller
avatar
grid-system
caching-application-block
misv

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