Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-08T07:10:13.790Z
Comments.Text: 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`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-08T08:05:05.990Z
Comments.Text: 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?
Comments.UserDisplayName: syntax-punk
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-08T08:18:22.573Z
Comments.Text: 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']`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-08T10:26:28.913Z
Comments.Text: Right. So entries are created some time ago, and yes I fetch em from DB based on some criteria, nothing fancy.
Comments.UserDisplayName: syntax-punk
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-08T11:34:38.987Z
Comments.Text: Could you please insert your code, maybe we get an insight was might be wrong.
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-09T09:40:33.300Z
Comments.Text: $criteria = craft()->elements->getCriteria(ElementType::Entry);
  
  
  $criteria->id = $ids;
  
  $criteria->limit = null;
  
  $criteria->with = array('someCoolFieldName');
  
  $entries = $criteria->find();
Comments.UserDisplayName: syntax-punk
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-09T09:42:18.577Z
Comments.Text: Basically Im getting an array of entries and traverse them, in order to fetch all neccesary fields.
Comments.UserDisplayName: syntax-punk
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-09T10:14:32.127Z
Comments.Text: 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.
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-09T10:34:13.250Z
Comments.Text: Yes thanks for help!
Comments.UserDisplayName: syntax-punk
Tags: <plugins><craft2>
Title: Why does $entry['slug'] and $entry->slug return different values?
ThreadText: # 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
  
  