Smarty: How to get value of array in Object - smarty

I have an array inside an object inside an array in my smarty template. A simplified example below, but using the same structure.
Array (
[product] => ModelView Object (
[model:protected] => Model Object (
[id] => 1234
)
)
)
How can I get [id]?
I can get the 'product' object to appear on the page as an array using:
$this->get_template_vars('product')
But from there I can't seem to narrow it down to only get the [id].

Apparently all it takes is a post on stackoverflow and then you try one more thing after hours and it works.
My solution below.
{php}
$id = $this->get_template_vars('product');
$id = $id->id;
{/php}

Related

CodeIgniter 3.1.7 unbuffered_row() returning NULL but row() works

I am attempting to output a CSV from a query result. dbutil->csv_from_result() should work, but it only returns the column headers. No data is included.
I tracked it down to system/database/DB_utility.php which uses unbuffered_row().
Any calls to unbuffered_row() return NULL. If I change it to row(), I get a result. It makes no difference if I use unbuffered_row('array') or unbuffered_row('object')
Why does row() work but unbuffered_row() does not?
Is this a bug in CI or am I missing something?
Edit: row('array') doesn't seem to work either.
Edit: It seems that calling $query->result() spoils dbutil->csv_from_result($query). You apparently cannot iterate through query results AND then save the results in a CSV file. This was possible in CI 2.
Is there any way to show query results AND save the CSV without running the query twice?
I have faced similar problem and found this:
I have this query:
$query = $this->db->query("SELECT * FROM tablename");
print_r($query);
Then I get this and unbuffered_row works fine:
CI_DB_oci8_result Object
(
[stmt_id] => Resource id #106
[curs_id] =>
[limit_used] =>
[commit_mode] => 32
[conn_id] => Resource id #91
[result_id] => 1
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[num_rows] =>
[row_data] =>
)
BUT if I call $query->num_rows() before I get a different CI_DB_oci8_result object and unbuffered_row didn't work returning null
$query = $this->db->query("SELECT * FROM tablename");
echo $query->num_rows();
print_r($query);
Hope this help somebody.

I've assigned Laravel Query Builder to a variable. It changes when being used

it's a WHY-question, not How-to one:)
I have assigned a Query Bulder to a variable $query:
$query = table::where(['id'=>1, 'this_version'=> 1]);
$versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);
outputs array with 2(!) sub-arrays:
Array
(
[slug1] => Array
(
[0] => Array
(
[tourist_id] => 1
[tourist_version] => 1
)
[1] => Array
(
[tourist_id] => 2
[tourist_version] => 1
)
)
)
But if I add another line using $query between my $query declaration and it's usage in getting $version[2] array, my $version[2] output is shortened to a 1-dimensional array:
$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
// Added line:
$versions['slug0'] = $query->select('version_created')->first()->version_created;
//
$versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);
outputs (note slug1 now has only 1 nested array):
Array
(
[slug0] => 2017-08-08 08:25:26
[slug1] => Array
(
[0] => Array
(
[tourist_id] => 1
[tourist_version] => 1
)
)
)
it seems like the like this line:
$versions['slug0'] = $query->select('version_created')->first()->version_created;
has added "first()" method to the original $query . Am I right and, if yes, why does it happen?
Well, this is because by default an object (in your case is the Query builder object) in PHP is passed by reference. You can read more about this here: PHP OOP References.
I quote from the above reference:
A PHP reference is an alias, which allows two different variables to
write to the same value.
When you pass the query builder object to the $query variable, you actually just pass the reference to this object and not the copy of it.
$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
So when you call the first() method on the second line, it actually modifies the query builder object.
$versions['slug0'] = $query->select('version_created')->first()->version_created;
Thus causing the upcoming query result to be limited to 1. In order to work around this issue, you can clone the query object like this:
$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
$versions['slug0'] = (clone $query)->select('version_created')->first()->version_created;
$versions['slug1'] = (clone $query)->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);
Hope this help!

laravel 5.4 validation array with keys

In laravel 5.4 when validation is failed I do like:
if ($validator->fails()) {
$errors_list = $validator->messages()->all();
and I got array like :
[errors_list] => Array
(
[0] => The image has already been taken.
[1] => The is main has already been taken.
)
What I dislike in this output that actuall name of error field is ommitted.
Code
echo '<pre>$validator->messages()::'.print_r($validator->messages(),true).'</pre>';
has output:
$validator->messages()::Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[image] => Array
(
[0] => The image has already been taken.
)
[is_main] => Array
(
[0] => The is main has already been taken.
)
)
[format:protected] => :message
)
And I did not find how access to messages data.
I would like to get array like:
[errors_list] => Array
(
[image] => The image has already been taken.
[is_main] => The is main has already been taken.
)
Is there is a way to make it ?
Thanks!
I hope this will work for you.
$errors->has('image');
$errors->get(image);
$errors->has('is_main');
$errors->get(is_main);
It's a tricky one. The reason it's like that is because there may be multiple validation errors happening at once. However if you only have a single rule per entry:
array_combine($validator->messages()->keys(),$validator->messages()->all())

laravel array_get first of multiple nested elements

i have an array, something like
[0] => Array
(
[id] => 1
[name] => Jimmy
[address] => Array
(
[number] => 1
[street] => Astreet
)
)
I need to access [street] using something like helper array_get dot notation:
array.address.street
However, as it can have multiple elements, I need something like
array*
where it can just get the first one.
Coming from cakephp they have a helper so i can do
array.{*}.address.street
Is there something similar in laravel, i cannot find such
If you want to get street from the first element you can do:
array_get(head($array), 'address.street');
// or
array_get($array, '0.address.street')
And if you want to get list of elements that contains street you can use array_pluck:
array_filter(array_pluck($array, 'address.street'));
$addresses = array_map(function($obj) {
if ( array_key_exists('address', $obj) )
if ( count($obj['address']) > 0 )
return $obj['address'][0];
}, $yourArray);

Simplexml How to access all element of the same node

[Villa] => Array
(
[0] => SimpleXMLElement Object
(
[VillaID] => 6
[VillaName] => Mary
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Sea
[Value] => 1000 m
)
[1] => SimpleXMLElement Object
(
[Destination] => Market
[Value] => 800 m
)
)
)
)
[1] => SimpleXMLElement Object
(
[VillaID] => 21
[VillaName] => Marion
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Beach
[Value] => 5 min
)
)
)
)
)
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
I can obtain it with:
echo 'Name of Villa: '.$xml->Villa[0]->VillaName.'';
etc. etc ... but in this way have to change manually for every villa (too much) the value in the brackets.
i've tried with $xml->Villa[$value]->VillaName; ($value comes from another page) but it's not working...
Tanks for help!
First of all, your question starts with "i have this xml:" followed by something that is not XML. I'm not saying this to be a smartass, rather because it's important for XML beginners to understand that print_r() is not the right way to inspect SimpleXMLElements. Sometimes it will show you things that aren't in your XML, other times it will not show things that are actually in your XML. In short: do not use print_r() on SimpleXMLElement. Just use ->asXML() and look at the actual XML.
From what I understand, you want to locate and select a node based on some criteria. XML just happens to have a language for that: XPath. The official specs aren't terribly user-friendly but w3schools.com has a pretty good XPath tutorial.
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
Anywhere in your document, you want to select all Villa nodes with an attribute VillaID whose value is "6". In XPath:
//Villa[#VillaID="6"]
Via SimpleXML:
$xml->xpath('//Villa[#VillaID="6"]');
Attention, xpath() always return an array.

Resources