douyu8187 2017-04-26 18:57
浏览 14
已采纳

返回并获取一个类中的对象

I am unable to write a code to get object for another function.

I have one class called RepairsService where I have two functions.

First createRepair($request, $vehicle) is where I create object.

public function createRepair($request, $vehicle)
    {
        DB::transaction(function () use ($request, $vehicle) {

            Log::info('Creating new repair.');

            $repair = new Repair();
            $repair->vehicle_id = $vehicle->id;

            DB::insert('insert into repairs (vehicle_id) values (?)', 
                    [$repair->vehicle_id]); 
        });
       $this->addRepair($request, $vehicle, $this); //here I call second function trying to pass $this parameter as object 
    }

And second in the same class is

public function addRepair($request, $vehicle, $repair){

        DB::transaction(function () use ($request, $vehicle, $repair) {

            $workers_needed->repair_id = $repair->id;  //I need to get this id from the object 
            $workers_needed->worker_id = $request->worker_id;

            DB::insert('insert into repair_worker (repair_id, worker_id) values (?, ?)',
                    [$workers_needed->repair_id, $workers_needed->worker_id]);

            Log::info('Repair created.');
            flash('Repair successfully added.', 'success');
        });
    }

What am I doing wrong? Right now using this code, I am getting error Undefined property: App\Models\Repairs\RepairsService::$id what makes sense, since I am not passing object (I think).

UPDATE:

So the main problem described above is fixed in the following code, but I am heading a very little problem now. In the second function, I am trying to assign $repair-id to $workers_needed->repair_id but I get NULL value there. I have tried to assign value 1 (hard coded) instead of using $repair-id but it seems that my second function call runs before the first function is done with transaction which makes sense, because as far as I know, transaction inserts data only if everything goes well and in this case, it is waiting for second function to get its work done. So the repair instance is not inserted into database. How can I edit the code so the transaction gets done and then I call my second function? Or is there another way how to do it?

public function createRepair($request, $vehicle)
    {
        $repair = null;
        DB::transaction(function () use ($request, $vehicle, $repair) {

            Log::info('Creating new repair.');

            $repair = new Repair();
            $repair->vehicle_id = $vehicle->id;

            DB::insert('insert into repairs (vehicle_id) values (?)', 
                    [$repair->vehicle_id]); 
            $this->addRepairWorker($request, $vehicle, $repair);
        });
    }


    public function addRepairWorker(Request $request , Vehicle $vehicle, Repair $repair){

        $workers_needed = null;
        DB::transaction(function () use ($request, $vehicle, $repair) {

            Log::info('Creating new repair worker instance.');

            $workers_needed = new Repair_worker();
            $workers_needed->repair_id = $repair->id;
            $workers_needed->worker_id = $request->worker_id;

            DB::insert('insert into repair_worker (repair_id, worker_id) values (?, ?)',
                    [$workers_needed->repair_id, $workers_needed->worker_id]);

            Log::info('Repair created.');
            flash('Repair successfully added.', 'success');
        });
    }
  • 写回答

2条回答 默认 最新

  • douweicheng5532 2017-04-26 19:02
    关注

    Should it not be;

    public function createRepair($request, $vehicle)
    {
        $repair = null;
        DB::transaction(function () use ($request, $vehicle, $repair) {
    
            Log::info('Creating new repair.');
    
            $repair = new Repair();
            $repair->vehicle_id = $vehicle->id;
    
            DB::insert('insert into repairs (vehicle_id) values (?)', 
                    [$repair->vehicle_id]); 
        });
       $this->addRepair($request, $vehicle, $repair);
    }
    

    This is because you are expecting a repair instance in that function call.

    Additionally, you are using an anonymous function so you have to declare repair outside of the scope and declare it for the function using use.

    UPDATE

    In order to accommodate for the new error, you need to commit() the transaction in order for it to updated/inserted. More information can be found here.

    DB::transaction(...);
    DB::commit(); // This is where the query actually gets sent to the database
    

    As you are rolling your own DB wrapper you may have to write the method yourself, an example of using PDO is below;

    class DB {
        ...
        public function commit() {
    
            return $PDO->commit();
        }
        ...
    }
    

    Commit returns a bool on success/failure so you can rollback if false or proceed when true.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀