dsj2222222 2018-11-27 20:58
浏览 66

Craft CMS - 如何在PHP条目查询中包含资产字段?

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: enter image description here

PHP Query results with asArray():

$entry_query = Entry::find()
->section('inventory')
->asArray();

Returns: enter image description here

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!

  • 写回答

1条回答 默认 最新

  • duanchoupo1104 2018-11-27 22:37
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂