We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Get Custom Field Shortnames with Model Service?

How Do I?

anthony.pinskey's avatar
anthony.pinskey
7 posts
7 years ago
anthony.pinskey's avatar anthony.pinskey

Version: EE4.0.9

Hi everyone!

I am trying to get all of the entries through my module and I noticed that when querying on the ChannelEntry Model, when accessing custom fields, the only way to get them is from the field_id and not their shortname.

Example:

public function get_entries()
    {
      $entries = ee('Model')->get('ChannelEntry')->filter('channel_id', 1)->all();
      foreach($entries as $entry) {
        var_dump($entry->field_id_1);
      }
    }

Is there a way to efficiently access the properties by their shortname/handle with something like

entry->my_custom_field_name
       
aaron.crawford's avatar
aaron.crawford
3 posts
7 years ago
aaron.crawford's avatar aaron.crawford

I asked this exact question in Slack chat today and the answer I got back was no. You have to reference $entry->field_id_1, unfortunately. May be a good feature request.

       
anthony.pinskey's avatar
anthony.pinskey
7 posts
7 years ago
anthony.pinskey's avatar anthony.pinskey

Hey thanks Aaron! I suppose I’ll consider doing a query for now if it’s efficient.

       
johnwick12's avatar
johnwick12
1 posts
7 years ago
johnwick12's avatar johnwick12

thanks alot

       
Andrés Molina's avatar
Andrés Molina
43 posts
5 years ago
Andrés Molina's avatar Andrés Molina

hi, for custom addon, im think create a private funcion query by “custom name” and return name in EE format, like “field_id_xx”

       
Andrés Molina's avatar
Andrés Molina
43 posts
5 years ago
Andrés Molina's avatar Andrés Molina

Like this, im try this and Works Fine(on my own Custom Addon)

Pass Custom Field name, and Return Custom Field ID

private function getfieldID($ci){
  $field = ee('Model')->get('ChannelField')
  ->filter('field_name', $ci) // or whatever you want/need to filter on
  ->first();
  
   return $field->field_id;
  
 }

In this function im filter Channel Entry by Channel ID and custom Field Value(member_id)

function add_product(){

  $this->member_id=ee()->session->userdata('member_id');
  $field_id=$this->getfieldID("cliente_id");
  
  $entries = ee('Model')->get('ChannelEntry')
   ->filter('channel_id', $this->channel_id) // or whatever you want/need to filter on
   ->filter('status', 'IN', array('open', 'other_status'))
  ->filter('field_id_'.$field_id, $this->member_id)
  
   ->order('title', 'ASC')
   ->all();
   
    
  
 foreach($entries->toArray() as $trow){
   
   print_r($trow);
  }
  $this->return_data="algo ss";
  return $this->channel_id;
 }



}
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.