<p>I'm writing a simple plugin for 'Craft 2' and It seems that a <code>slug</code> of an entry is returned if you reffer it like: <code>$entry['slug']</code> but calling <code>$entry-&gt;slug</code> returns same value as entry title. </p>

<p>Is there an explanation of why it happens and what is the best practice of getting values of the entry fields with a homemade plugin?</p>

<p>Thanks!</p>

<p>God bless ya'll. </p>

CC BY-SA 4.0
2019-04-08T07:10:13.790Z
Usually `$entry->slug` is the correct way. `$entry['slug']` will trigger the `Arrayable` trait and tries to cast your object into an array. Do you do something with your entry or do you change certain values? Is it right after you create a new entry or do you fetch it from your DB? Best practice is to use the object syntax -> `$entry->slug`
Robin Schambach
CC BY-SA 4.0
2019-04-08T08:05:05.990Z
The idea is more like; build a JSON and then send it to another endpoint (call it REST API). And I wanna specify fields which I'm interested in, so they should be customizable. For this kind of scenario do you have a 100% working approach?
syntax-punk
CC BY-SA 4.0
2019-04-08T08:18:22.573Z
Yes I do. But you didn't answer my questions at all. It doesn't matter what you want to do with the data, all that matters is what you did before you call `$entry['slug']`
Robin Schambach
CC BY-SA 4.0
2019-04-08T10:26:28.913Z
Right. So entries are created some time ago, and yes I fetch em from DB based on some criteria, nothing fancy.
syntax-punk
CC BY-SA 4.0
2019-04-08T11:34:38.987Z
Could you please insert your code, maybe we get an insight was might be wrong.
Robin Schambach
CC BY-SA 4.0
2019-04-09T09:40:33.300Z
$criteria = craft()->elements->getCriteria(ElementType::Entry);


$criteria->id = $ids;

$criteria->limit = null;

$criteria->with = array('someCoolFieldName');

$entries = $criteria->find();
syntax-punk
CC BY-SA 4.0
2019-04-09T09:42:18.577Z
Basically Im getting an array of entries and traverse them, in order to fetch all neccesary fields.
syntax-punk
CC BY-SA 4.0
2019-04-09T10:14:32.127Z
Under the hood Craft does many things automatically and converts many things automatically for you. `$entry->slug` should work for sure.. that's why I asked for your complete code.
Robin Schambach
CC BY-SA 4.0
2019-04-09T10:34:13.250Z
Yes thanks for help!
syntax-punk
<plugins><craft2>
Why does $entry['slug'] and $entry->slug return different values?
# Why does $entry['slug'] and $entry->slug return different values?

(Asked by: username_0 on 2019-04-08)

I'm writing a simple plugin for 'Craft 2' and It seems that a `slug` of an entry is returned if you reffer it like: `$entry['slug']` but calling `$entry->slug` returns same value as entry title.

Is there an explanation of why it happens and what is the best practice of getting values of the entry fields with a homemade plugin?

Thanks!

God bless ya'll.

**Tags:** plugins, craft2
> **Comments:**
> **username_1**: Usually `$entry->slug` is the correct way. `$entry['slug']` will trigger the `Arrayable` trait and tries to cast your object into an array. Do you do something with your entry or do you change certain values? Is it right after you create a new entry or do you fetch it from your DB? Best practice is to use the object syntax -> `$entry->slug`  
> **username_0**: The idea is more like; build a JSON and then send it to another endpoint (call it REST API). And I wanna specify fields which I'm interested in, so they should be customizable. For this kind of scenario do you have a 100% working approach?  
> **username_1**: Yes I do. But you didn't answer my questions at all. It doesn't matter what you want to do with the data, all that matters is what you did before you call `$entry['slug']`  
> **username_0**: Right. So entries are created some time ago, and yes I fetch em from DB based on some criteria, nothing fancy.  
> **username_1**: Could you please insert your code, maybe we get an insight was might be wrong.  
> **username_0**: $criteria = craft()->elements->getCriteria(ElementType::Entry);   $criteria->id = $ids;  $criteria->limit = null;  $criteria->with = array('someCoolFieldName');  $entries = $criteria->find();  
> **username_0**: Basically Im getting an array of entries and traverse them, in order to fetch all neccesary fields.  
> **username_1**: Under the hood Craft does many things automatically and converts many things automatically for you. `$entry->slug` should work for sure.. that's why I asked for your complete code.  
> **username_0**: Yes thanks for help!  

---

## 0 answers

