Welcome Guest, Not a member yet? Register   Sign In
builder's set() captured by model doesn't support object
#1

The documentation for update() says I can use
PHP Code:
$userModel
    
->whereIn('id', [1,2,3])
    ->set(['active' => 1])
    ->update(); 
and the documentation for $builder->set() says set() also accepts objects, but it appears the set() on models doesn't support objects
Reply
#2

PHP Code:
<?php namespace Modules\Common\Models;


use 
Modules\Common\Entities\ChatPrivateMediaEntity;

use 
CodeIgniter\Model;

class  ChatPrivateMediaModel extends Model
{
    protected $table 'chat_private_media';
    protected $primaryKey 'id';

    protected $returnType ChatPrivateMediaEntity::class;
    protected $allowedFields = [
        'image',
        'chat_private_id',
        'path',


    ];


    protected $validationRules = [

    ];
    protected $validationMessages = [];
    protected $skipValidation false;



PHP Code:
<?php

namespace Modules\Common\Controllers;


use 
Modules\Common\Entities\ChatPrivateMediaEntity;

use 
Modules\Common\Libraries\CustomFileSystem;


use 
Modules\Common\Models\ChatPrivateMediaModel;

use 
CodeIgniter\HTTP\ResponseInterface;
use 
Modules\Shared\Controllers\ApiController;


class 
ChatPrivateMedia extends ApiController
{


    /**
    * index function
    * @method : GET
    */
    public function index()
    {

        $chatPrivateMediaModel = new ChatPrivateMediaModel();


        $result $chatPrivateMediaModel->select($this->urlQueryParam->getFiled())
    
            
->orderBy($this->urlQueryParam->getSort(), $this->urlQueryParam->getOrder())
            ->paginate($this->urlQueryParam->getLimit(), 'default'$this->urlQueryParam->getPage(), $this->urlQueryParam->getOffset());


        return $this->respond([
            'data' => $result,
            'pager' => $chatPrivateMediaModel->pager->getDetails()
        ], ResponseInterface::HTTP_OKlang('Shared.api.receive'));

    }

    /**
    * show function
    * @method : GET with params ID
    */
    public function show($id null)
    {
        $chatPrivateMediaModel = new ChatPrivateMediaModel();


        return $this->respond([

            'data' => $chatPrivateMediaModel->where('id'$id)->paginate(1'default'),
            'pager' => $chatPrivateMediaModel->pager->getDetails()
        ], ResponseInterface::HTTP_OKlang('Shared.api.receive'));

    }

    /**
    * create function
    * @method : POST
    */
    public function create()

    {
        $chatPrivateMediaModel = new ChatPrivateMediaModel();
        $customConfig = new \Modules\Common\Config\ModuleCommonConfig();
        $imageService = \CodeIgniter\Config\Services::image();

        $chatPrivateMediaEntity = new ChatPrivateMediaEntity();


        if ($this->request->getPost()) {

            $rules = [
                'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,webp,jpeg,jpg,gif]',
                'chatPrivateId' => 'required'
            ];
            if (!$this->validate($rules)) {

                return $this->respond([
                    'error' => $this->validator->getErrors(),
                    'success' => false
                
], ResponseInterface::HTTP_NOT_ACCEPTABLElang('Shared.api.validation'));

            }

            $chatPrivateMediaEntity->chatPrivateId $this->request->getPost('chatPrivateId');
            if (isset($_FILES['image'])) {

                foreach ($this->request->getFileMultiple('image') as $avatar) {

                    $avatar->move($customConfig->uploadDirectory '/chat_private'time() . '.' $avatar->getClientExtension());


                    $chatPrivateMediaEntity->path $avatar->getName();
                    $chatPrivateMediaEntity->editPath();
                    if ($avatar->getClientExtension() != 'gif') {
                        $imageService->withFile(ROOTPATH $chatPrivateMediaEntity->path)
                            ->withResource()
                            ->save(ROOTPATH $chatPrivateMediaEntity->path90);

                    }


                    if (!$chatPrivateMediaModel->save($chatPrivateMediaEntity)) {

                        return $this->respond([
                            'error' => $chatPrivateMediaModel->errors(),
                            'success' => false,
                        ], ResponseInterface::HTTP_BAD_REQUESTlang('Shared.api.reject'));

                    }

                }
            }


            return $this->respond([
                'data' => ''
            ], ResponseInterface::HTTP_CREATEDlang('Shared.api.save'));

        }


    }

    /**
    * update function
    * @method : PUT or PATCH
    */
    public function update($id null)
    {

        $chatPrivateMediaModel = new ChatPrivateMediaModel();
        $customConfig = new \Modules\Common\Config\ModuleCommonConfig();
        $imageService = \CodeIgniter\Config\Services::image();
        $chatPrivateMediaEntity = new ChatPrivateMediaEntity();
        $handy = new CustomFileSystem();

        $chatRoomMedia null;
        if ($this->request->getPost()) {

            $rules = [
                'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,jpg,webp,gif]',
            'chatPrivateId' => 'required'
            ];

            if (!$this->validate($rules)) {

                return $this->respond([
                    'error' => $this->validator->getErrors(),
                    'success' => false
                
], ResponseInterface::HTTP_NOT_ACCEPTABLElang('Shared.api.validation'));

            }


            $chatRoomMedia $chatPrivateMediaModel->where('id'$id)->first();

            if (is_null($chatRoomMedia)) {
                return $this->respond([
                    'error' => $this->validator->getErrors(),
                    'success' => false
                
], ResponseInterface::HTTP_NOT_FOUNDlang('Shared.api.exist'));

            }


            $chatPrivateMediaEntity->chatPrivateId $this->request->getPost('chatPrivateId');
            $chatPrivateMediaEntity->id $id;
            if (isset($_FILES['image'])) {


                foreach ($this->request->getFileMultiple('image') as $avatar) {

                    $avatar->move($customConfig->uploadDirectory '/chat_private'time() . '.' $avatar->getClientExtension());

                    $chatPrivateMediaEntity->path $avatar->getName();
                    $chatPrivateMediaEntity->editPath();
                    if ($avatar->getClientExtension() != 'gif') {
                        $imageService->withFile(ROOTPATH $chatPrivateMediaEntity->path)
                            ->withResource()
                            ->save(ROOTPATH $chatPrivateMediaEntity->path90);


                    }


                    if (!$chatPrivateMediaModel->update($id$chatPrivateMediaEntity)) {

                        return $this->respond([
                            'error' => $chatPrivateMediaModel->errors(),
                            'success' => false,
                        ], ResponseInterface::HTTP_BAD_REQUESTlang('Shared.api.reject'));

                    }

                }
            }

            $handy->removeSingleFile(ROOTPATH $chatRoomMedia->path);
            return $this->respond([
                'data' => array(['id' => $id,
                    'chatPrivateId' => $chatRoomMedia->chatPrivateId,
                    'path' => $chatPrivateMediaEntity->path])
            ], ResponseInterface::HTTP_OKlang('Shared.api.update'));
        }
    }

    /**
    * edit function
    * @method : DELETE with params ID
    */
    public function delete($id null)
    {

        $chatPrivateMediaModel = new ChatPrivateMediaModel();
        $handy = new CustomFileSystem();


        $id = ($id == $id);

        if ($id == 0) {

            $isExist $chatPrivateMediaModel->where(['chat_private_id' => $this->urlQueryParam->getForeignKey()])->
            findAll();
            $target = array('chat_private_id' => $this->urlQueryParam->getForeignKey());
        } else {
            $isExist $chatPrivateMediaModel->where(['id' => $id])->findAll();
            $target = array('id' => $id);
        }


        if ($isExist) {
            $chatPrivateMediaModel->where($target)->delete();
            foreach ($isExist as $path) {

                $handy->removeSingleFile(ROOTPATH $path->path);
            }


        }


        return $this->respond([
        ], ResponseInterface::HTTP_OKlang('Shared.api.remove'));


    }


Enlightenment  Is  Freedom
Reply




Theme © iAndrew 2016 - Forum software by © MyBB