Craft CMS - How to include asset fields in PHP entry query?
I am trying to figure out how to return asset field(s) in a PHP entry query. Also if I could learn how to return "custom fields" when returning an Object that would be great too! Right now I am having to specify asArray() to even get access to most of my "custom fields"
As as example: I have a Vehicle Entry which has a custom field with the handle of price (number field) and another custom field (asset field) with the handle of images. When I execute the query without specifying the asArray() param I cannot find the custom fields included in the results. But if I specify asArray() then they are all there with the exception of my images field which I think is because it is an asset field or possible because it can be a collection of images? How can I make sure all the fields tied to an entry are returned in my query?
Here are some examples of queries and the corresponding results:
PHP Query without asArray():
$entry_query = Entry::find()
->section('inventory')
->all();
Returns:
PHP Query results with asArray():
$entry_query = Entry::find()
->section('inventory')
->asArray();
Returns:
However even when specifing to make the result set an array I still cannot figure out how to include the 'images' field.
I am having a difficult time finding an answer via the documentation or an example of someone doing the same. All the examples i find are for the template side in twig.
Thanks!
php database activerecord orm craftcms
add a comment |
I am trying to figure out how to return asset field(s) in a PHP entry query. Also if I could learn how to return "custom fields" when returning an Object that would be great too! Right now I am having to specify asArray() to even get access to most of my "custom fields"
As as example: I have a Vehicle Entry which has a custom field with the handle of price (number field) and another custom field (asset field) with the handle of images. When I execute the query without specifying the asArray() param I cannot find the custom fields included in the results. But if I specify asArray() then they are all there with the exception of my images field which I think is because it is an asset field or possible because it can be a collection of images? How can I make sure all the fields tied to an entry are returned in my query?
Here are some examples of queries and the corresponding results:
PHP Query without asArray():
$entry_query = Entry::find()
->section('inventory')
->all();
Returns:
PHP Query results with asArray():
$entry_query = Entry::find()
->section('inventory')
->asArray();
Returns:
However even when specifing to make the result set an array I still cannot figure out how to include the 'images' field.
I am having a difficult time finding an answer via the documentation or an example of someone doing the same. All the examples i find are for the template side in twig.
Thanks!
php database activerecord orm craftcms
add a comment |
I am trying to figure out how to return asset field(s) in a PHP entry query. Also if I could learn how to return "custom fields" when returning an Object that would be great too! Right now I am having to specify asArray() to even get access to most of my "custom fields"
As as example: I have a Vehicle Entry which has a custom field with the handle of price (number field) and another custom field (asset field) with the handle of images. When I execute the query without specifying the asArray() param I cannot find the custom fields included in the results. But if I specify asArray() then they are all there with the exception of my images field which I think is because it is an asset field or possible because it can be a collection of images? How can I make sure all the fields tied to an entry are returned in my query?
Here are some examples of queries and the corresponding results:
PHP Query without asArray():
$entry_query = Entry::find()
->section('inventory')
->all();
Returns:
PHP Query results with asArray():
$entry_query = Entry::find()
->section('inventory')
->asArray();
Returns:
However even when specifing to make the result set an array I still cannot figure out how to include the 'images' field.
I am having a difficult time finding an answer via the documentation or an example of someone doing the same. All the examples i find are for the template side in twig.
Thanks!
php database activerecord orm craftcms
I am trying to figure out how to return asset field(s) in a PHP entry query. Also if I could learn how to return "custom fields" when returning an Object that would be great too! Right now I am having to specify asArray() to even get access to most of my "custom fields"
As as example: I have a Vehicle Entry which has a custom field with the handle of price (number field) and another custom field (asset field) with the handle of images. When I execute the query without specifying the asArray() param I cannot find the custom fields included in the results. But if I specify asArray() then they are all there with the exception of my images field which I think is because it is an asset field or possible because it can be a collection of images? How can I make sure all the fields tied to an entry are returned in my query?
Here are some examples of queries and the corresponding results:
PHP Query without asArray():
$entry_query = Entry::find()
->section('inventory')
->all();
Returns:
PHP Query results with asArray():
$entry_query = Entry::find()
->section('inventory')
->asArray();
Returns:
However even when specifing to make the result set an array I still cannot figure out how to include the 'images' field.
I am having a difficult time finding an answer via the documentation or an example of someone doing the same. All the examples i find are for the template side in twig.
Thanks!
php database activerecord orm craftcms
php database activerecord orm craftcms
edited Dec 3 '18 at 20:29
Paras
5591823
5591823
asked Nov 27 '18 at 20:58
RadmationRadmation
965720
965720
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is what I ended up with:
$vehicles = array(); // empty container to hold our modified entries
// Lets Query our Inventory - all of it
/** @var array $entry_query the name of our query to get our inventory - return a list of inventory after we execute query */
$entries = Entry::find()
->section('inventory')
->all();
foreach($entries as $entry) {
/*
* Let's get all our custom fields we want
* to include with our entries
*/
// Get our image field and add to result set - because it's an asset field this returns a query object
$ourimages = $entry->images->all(); // get all our images
$price = $entry->price;
$featured = $entry->featureThisVehicle;
$make = $entry->make;
$model = $entry->model;
$year = $entry->year;
$description = $entry->description;
$inventoryStatus = $entry->inventoryStatus;
$bodyStyle = $entry->bodyStyle;
$color = $entry->color;
$miles = $entry->miles;
$vin = $entry->vin;
$stkid = $entry->stkid;
// cast out entry object as an array - so we can add props to it
$entry = (array)$entry;
// add our custom fields to our newly casted entry array
$entry['images'] = $ourimages;
$entry['price'] = $price;
$entry['featured'] = $featured;
$entry['make'] = $make;
$entry['model'] = $model;
$entry['year'] = $year;
$entry['description'] = $description;
$entry['inventoryStatus'] = $inventoryStatus;
$entry['bodyStyle'] = $bodyStyle;
$entry['color'] = $color;
$entry['miles'] = $miles;
$entry['vin'] = $vin;
$entry['stkid'] = $stkid;
// Recast back to object just cause (not really necessary since we are json_encode'ing this)
$entry = (object)$entry;
array_push($vehicles, $entry);
}
return json_encode($vehicles);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508038%2fcraft-cms-how-to-include-asset-fields-in-php-entry-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is what I ended up with:
$vehicles = array(); // empty container to hold our modified entries
// Lets Query our Inventory - all of it
/** @var array $entry_query the name of our query to get our inventory - return a list of inventory after we execute query */
$entries = Entry::find()
->section('inventory')
->all();
foreach($entries as $entry) {
/*
* Let's get all our custom fields we want
* to include with our entries
*/
// Get our image field and add to result set - because it's an asset field this returns a query object
$ourimages = $entry->images->all(); // get all our images
$price = $entry->price;
$featured = $entry->featureThisVehicle;
$make = $entry->make;
$model = $entry->model;
$year = $entry->year;
$description = $entry->description;
$inventoryStatus = $entry->inventoryStatus;
$bodyStyle = $entry->bodyStyle;
$color = $entry->color;
$miles = $entry->miles;
$vin = $entry->vin;
$stkid = $entry->stkid;
// cast out entry object as an array - so we can add props to it
$entry = (array)$entry;
// add our custom fields to our newly casted entry array
$entry['images'] = $ourimages;
$entry['price'] = $price;
$entry['featured'] = $featured;
$entry['make'] = $make;
$entry['model'] = $model;
$entry['year'] = $year;
$entry['description'] = $description;
$entry['inventoryStatus'] = $inventoryStatus;
$entry['bodyStyle'] = $bodyStyle;
$entry['color'] = $color;
$entry['miles'] = $miles;
$entry['vin'] = $vin;
$entry['stkid'] = $stkid;
// Recast back to object just cause (not really necessary since we are json_encode'ing this)
$entry = (object)$entry;
array_push($vehicles, $entry);
}
return json_encode($vehicles);
add a comment |
This is what I ended up with:
$vehicles = array(); // empty container to hold our modified entries
// Lets Query our Inventory - all of it
/** @var array $entry_query the name of our query to get our inventory - return a list of inventory after we execute query */
$entries = Entry::find()
->section('inventory')
->all();
foreach($entries as $entry) {
/*
* Let's get all our custom fields we want
* to include with our entries
*/
// Get our image field and add to result set - because it's an asset field this returns a query object
$ourimages = $entry->images->all(); // get all our images
$price = $entry->price;
$featured = $entry->featureThisVehicle;
$make = $entry->make;
$model = $entry->model;
$year = $entry->year;
$description = $entry->description;
$inventoryStatus = $entry->inventoryStatus;
$bodyStyle = $entry->bodyStyle;
$color = $entry->color;
$miles = $entry->miles;
$vin = $entry->vin;
$stkid = $entry->stkid;
// cast out entry object as an array - so we can add props to it
$entry = (array)$entry;
// add our custom fields to our newly casted entry array
$entry['images'] = $ourimages;
$entry['price'] = $price;
$entry['featured'] = $featured;
$entry['make'] = $make;
$entry['model'] = $model;
$entry['year'] = $year;
$entry['description'] = $description;
$entry['inventoryStatus'] = $inventoryStatus;
$entry['bodyStyle'] = $bodyStyle;
$entry['color'] = $color;
$entry['miles'] = $miles;
$entry['vin'] = $vin;
$entry['stkid'] = $stkid;
// Recast back to object just cause (not really necessary since we are json_encode'ing this)
$entry = (object)$entry;
array_push($vehicles, $entry);
}
return json_encode($vehicles);
add a comment |
This is what I ended up with:
$vehicles = array(); // empty container to hold our modified entries
// Lets Query our Inventory - all of it
/** @var array $entry_query the name of our query to get our inventory - return a list of inventory after we execute query */
$entries = Entry::find()
->section('inventory')
->all();
foreach($entries as $entry) {
/*
* Let's get all our custom fields we want
* to include with our entries
*/
// Get our image field and add to result set - because it's an asset field this returns a query object
$ourimages = $entry->images->all(); // get all our images
$price = $entry->price;
$featured = $entry->featureThisVehicle;
$make = $entry->make;
$model = $entry->model;
$year = $entry->year;
$description = $entry->description;
$inventoryStatus = $entry->inventoryStatus;
$bodyStyle = $entry->bodyStyle;
$color = $entry->color;
$miles = $entry->miles;
$vin = $entry->vin;
$stkid = $entry->stkid;
// cast out entry object as an array - so we can add props to it
$entry = (array)$entry;
// add our custom fields to our newly casted entry array
$entry['images'] = $ourimages;
$entry['price'] = $price;
$entry['featured'] = $featured;
$entry['make'] = $make;
$entry['model'] = $model;
$entry['year'] = $year;
$entry['description'] = $description;
$entry['inventoryStatus'] = $inventoryStatus;
$entry['bodyStyle'] = $bodyStyle;
$entry['color'] = $color;
$entry['miles'] = $miles;
$entry['vin'] = $vin;
$entry['stkid'] = $stkid;
// Recast back to object just cause (not really necessary since we are json_encode'ing this)
$entry = (object)$entry;
array_push($vehicles, $entry);
}
return json_encode($vehicles);
This is what I ended up with:
$vehicles = array(); // empty container to hold our modified entries
// Lets Query our Inventory - all of it
/** @var array $entry_query the name of our query to get our inventory - return a list of inventory after we execute query */
$entries = Entry::find()
->section('inventory')
->all();
foreach($entries as $entry) {
/*
* Let's get all our custom fields we want
* to include with our entries
*/
// Get our image field and add to result set - because it's an asset field this returns a query object
$ourimages = $entry->images->all(); // get all our images
$price = $entry->price;
$featured = $entry->featureThisVehicle;
$make = $entry->make;
$model = $entry->model;
$year = $entry->year;
$description = $entry->description;
$inventoryStatus = $entry->inventoryStatus;
$bodyStyle = $entry->bodyStyle;
$color = $entry->color;
$miles = $entry->miles;
$vin = $entry->vin;
$stkid = $entry->stkid;
// cast out entry object as an array - so we can add props to it
$entry = (array)$entry;
// add our custom fields to our newly casted entry array
$entry['images'] = $ourimages;
$entry['price'] = $price;
$entry['featured'] = $featured;
$entry['make'] = $make;
$entry['model'] = $model;
$entry['year'] = $year;
$entry['description'] = $description;
$entry['inventoryStatus'] = $inventoryStatus;
$entry['bodyStyle'] = $bodyStyle;
$entry['color'] = $color;
$entry['miles'] = $miles;
$entry['vin'] = $vin;
$entry['stkid'] = $stkid;
// Recast back to object just cause (not really necessary since we are json_encode'ing this)
$entry = (object)$entry;
array_push($vehicles, $entry);
}
return json_encode($vehicles);
answered Nov 27 '18 at 22:37
RadmationRadmation
965720
965720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508038%2fcraft-cms-how-to-include-asset-fields-in-php-entry-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown