我可以在CakePHP中以更简单的方式处理记录吗?

我可以在CakePHP中以更简单的方式处理记录吗?,cakephp,cakephp-2.1,Cakephp,Cakephp 2.1,我开始处理CakePHP并阅读文档,但有两件事对我来说仍然有点笨拙 我知道其他框架中有我想存储的特定的记录,但CakePHP建议我匿名存储: $this->Foo->create(array(...)); $this->Foo->save(); 为什么我不能告诉CakePHP要保存哪个记录,就像在其他框架中一样: $foo = $this->Foo->create(array(...)); $foo->save(); 我希望遍历控制器中的整个记录集

我开始处理CakePHP并阅读文档,但有两件事对我来说仍然有点笨拙

  • 我知道其他框架中有我想存储的特定的记录,但CakePHP建议我匿名存储:

    $this->Foo->create(array(...));
    $this->Foo->save();
    
    为什么我不能告诉CakePHP要保存哪个记录,就像在其他框架中一样:

    $foo = $this->Foo->create(array(...));
    $foo->save();
    
  • 我希望遍历控制器中的整个记录集。为什么我需要使用

    $foos = $this->Foos->find('all');
    foreach($foos as $foo){
       $foo['Foo'] // ... here we have $foo.
    
    我不明白为什么
    find()。为什么这不是一个直接的记录数组

  • $this->Foo是Foo模型的一个实例。当您对其调用方法时,您正在对该Foo模型实例的活动记录(如果有)调用方法。因此,在告诉Cake保存哪个记录方面,您不需要-Cake知道如何保存当前活动记录
  • 下面是您粘贴了注释的代码,这可能会有所帮助

    // Prepare this instance of the Foo model to save a new record
    $this->Foo->create(array(...));
    
    // Save the new record that we have just prepared
    $this->Foo->save();
    
    另一方面

    // Call the create method on this instance of the Foo model, and return what?
    // Return another instance of the Foo model?
    // Why not just continue using the instance we already have, ie, $this->Foo
    $foo = $this->Foo->create(array(...));
    
    // Call the save method on the duplicate instance of the Foo model that was
    // returned from the create method?
    $foo->save(); 
    
    // Why did 'create' need to return a duplicate instance of the model to do a save???
    // Why not call the save on the same instance of the Foo model that we used to call the create?
    
    第2点。这基本上是为了一致性。通常,您将从多个相互链接的表返回数据。假设表Foo和Bar具有1对1的关系,您将获得Foo记录及其关联的Bar记录。返回的数组将需要Foo和Bar键,例如:在您的foreach循环中,$Foo可能包含:

    $foo['foo']['column1'],$foo['foo']['column2'],$foo['Bar']['column1'],$foo['Bar']['column2']

    为了保持一致,当您仅从一个表中获取数据时,它仍然以$foo['foo']['column1']的形式返回,就像从多个表中获取关联数据一样

    编辑:回应你的评论,说你有代码:

    $foos = $this->Foos->find('all');
    
    假设您想对返回数组的每一行调用某个模型方法,有几种方法可以做到这一点。一种方法是:

    // This is code for the controller
    $this->Car->find('all');
    foreach($cars as $car){
        $this->Car->driveTwoMiles($car); // the driveTwoMiles would be in your model class
    
    }
    
    因此,在您的模型中,您有一个方法:

    // This would be a method in your model class
    function driveTwoMiles($car){
        $this->id = $car['Car']['id']; // set the active record
        // we are now inside the model, so $this->id is the same as calling $this->Car->id from the controller
    
        // Do whatever you want here. You have an active record, and your $car variable, holding data
        $this->Post->saveField('distance_driven', $car['Car']['distance_driven']+2);
    }
    
    此外,对于只想更新一条记录而不是多条记录的情况,您可以只进行“读取”而不是“查找('all')”——更多信息请参见下面的链接

    我强烈建议您通读蛋糕烹饪书中的以下几页:

    -检索数据

    -保存数据

    -删除数据


    所有这些都包含关于如何使用蛋糕模型的非常重要的基础信息。现在就花点时间真正理解它,你将在将来为自己省去无数的麻烦和重新编码的因素

    谢谢你详细的回答!第2点:但这些仍然只是原始数据数组,其中没有任何对象。这样我就不能调用模型的任何方法,比如:
    $car->driveTwoMiles()
    $car->getDrivers()
    。我不能用CakePHP做这个吗?我刚刚重新编辑了我的编辑。。。抱歉,如果你在我编辑的时候读到了这篇文章,但现在它应该更有意义了!