简体   繁体   English

试图在Kohana php框架中添加ORM对象的“未定义索引”

[英]“Undefined index” trying to add ORM object in Kohana php framework

I'm trying to add an ORM object to another one (one to many relationship) in Kohana: 我正在尝试向Kohana中的另一个对象(一对多关系)添加ORM对象:

$item = $cart->cartitems->where('productid', '=', $product->id);

    //if($item == null)//This apparently doesn't work in Kohana, it returns an object, even if not loaded, using the following:
    if (!$item->loaded()) {
        $item = new Model_Cartitem();
        $item->productid = $product->id;
        $item->quantity = 1; //TODO: Change to incrementation
        $item->totalprice = $product->price; //TODO: Change to incrementation
        $item->unitprice = $product->price;
        $item->productid = $product->id;
        $item->productnumber = $product->productnumber;
        $cart->add('cartitem', $item);
    }


    $cart->save();

Here's the cart class: 这是购物车类:

class Model_Cart extends ORM
{
    protected $_has_many = array('cartitems' => array());
}

and the cartitem class: 和Cartitem类:

class Model_Cartitem extends ORM {
    protected $_belongs_to = array('cart' => array('foreign_key' => 'cartid'));
}

But when I run it I get the error "ErrorException [ 8 ]: Undefined index: cartitem ~ MODPATH\\orm\\classes\\kohana\\orm.php [ 1403 ]" 但是,当我运行它时,出现错误“ ErrorException [8]:未定义索引:Cartitem〜MODPATH \\ orm \\ classes \\ kohana \\ orm.php [1403]”

What does this "undefined index" refer to, and how do I fix this? 这个“未定义索引”指的是什么,我该如何解决?

EDIT: 编辑:

Changed the belongs_to in Model_Cartitem. 更改了Model_Cartitem中的belongs_to。 Here is the definition of my foreign key in table cartitems (from NaviCat): 这是表Cartitems中的外键的定义(来自NaviCat):

Cartitems表中的外键

EDIT 2: 编辑2:

Here's the sql code for the tables: 这是表的sql代码:

cartitems: Cartitems:

CREATE TABLE `cartitems` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `productid` INT(11) NOT NULL,
    `quantity` INT(11) NOT NULL,
    `totalprice` DOUBLE NOT NULL,
    `cart_id` INT(11) NOT NULL,
    `productname` TEXT NOT NULL,
    `unitprice` DOUBLE NOT NULL,
    `productnumber` TEXT NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `cart_id` (`cart_id`),
    CONSTRAINT `cart_id` FOREIGN KEY (`cart_id`) REFERENCES `carts` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2

carts: 推车:

CREATE TABLE `carts` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `username` TEXT NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2

And since I changed the foreign key id name to cart_id I would think I should be able to use the simpler relationship definitions in the Model classes, so I changed to this: 并且由于我将外键ID名称更改为cart_id,所以我认为我应该能够在Model类中使用更简单的关系定义,所以我改为:

class Model_Cart extends ORM
{
    protected $_has_many = array('cartitems' => array());
}

and

class Model_Cartitem extends ORM
{
    protected $_belongs_to = array('cart' => array());
}

Still doesn't work, still getting the undefined index: cartitem error... 仍然不起作用,仍然得到未定义的索引:Cartitem错误...

EDIT 3: 编辑3:

Ok, so I tried doing the exact same thing in Asp.Net MVC 3, using the Entity Framework as ORM, just to see if there was something wrong with the database table definitions. 好的,所以我尝试使用Entity Framework作为ORM在Asp.Net MVC 3中执行完全相同的操作,只是为了查看数据库表定义是否存在问题。 But everything worked just fine there. 但是在那里一切正常。

So at least I know now it has nothing to do with the database being faulty. 所以至少我现在知道这与数据库故障无关。 So the problem must lie in the Kohana ORM and I must be doing something wrong there. 因此,问题必须出在Kohana ORM上,而我在那一定做错了。 I tried the suggestions from Yoda, but nothing has helped so far. 我尝试了Yoda的建议,但到目前为止没有任何帮助。 This is getting frustratingly difficult, maybe I should look at CodeIgniter instead, which is supposed to be simpler... But I've kind of liked Kohana in other ways. 这变得越来越令人沮丧,也许我应该去看看CodeIgniter,它应该更简单...但是我在其他方面有点喜欢Kohana。 Is there really no one who knows what the problem with my ORM classes is? 真的没有人知道我的ORM类存在什么问题吗?

BTW: Is this part wrong? 顺便说一句:这部分错了吗? $cart->add('cartitem', $item); $ cart-> add('cartitem',$ item); It's the only way I've found to add a relationship object, but at the same time, it says in the docs that you can add relationships in a many-to-many relationship this way... But if it doesn't apply to one-to-many, then how would I add the new cartitem to the cart? 这是我发现添加关系对象的唯一方法,但与此同时,它在文档中说您可以通过这种方式在多对多关系中添加关系...但是,如果它不适用一对多,那么我如何将新的Cartitem添加到购物车?

Ok, so maybe I figured it out... If I'm right, it had nothing to do with my database being wrong, or the Model class relationships being wrongly defined. 好的,所以也许我想出了...如果我是对的,那与我的数据库错误或模型类关系错误定义无关。 Rather, it seems to me that you couldn't actually use the add() method on a one-to-many relationship. 相反,在我看来,您实际上无法在一对多关系上使用add()方法。 It seems that to add a related object to a collection of a parent object you simply save the object, and manually add the foreign key id (cart_id) to relate them. 似乎要将相关对象添加到父对象的集合中,您只需保存该对象,然后手动添加外键ID(cart_id)即可将其关联。 If so, this is the difference from Asp.Net MVC and Entity Framework that I had a hard time grasping, because there you don't care about the foreign key, it is added automatically when you add an object to the child collection, like this in Asp.Net MVC: cart.cartitems.Add(item); 如果是这样,这与我很难掌握的Asp.Net MVC和Entity Framework有所不同,因为您不必在意外键,当您将一个对象添加到子集合时,它会自动添加,例如这在Asp.Net MVC中:cart.cartitems.Add(item);

Oh, and also, when looping through the child collection, it seems you can't just do this: 哦,而且,当循环遍历子集合时,似乎不能仅仅这样做:

foreach ($cart->cartitems as $item)

As I would have expected... But when I add a find_all() it works: 正如我所期望的...但是当我添加find_all()时,它可以工作:

foreach ($cart->cartitems->find_all() as $item)

If this is not the correct answer, and there is in fact a way to add a child object with a special method, and the foreign key id is taken care of automatically, I would love to hear it, because that would make me more comfortable, but if not I hope this helps someone else trying to transition from Asp.Net MVC... 如果这不是正确的答案,并且实际上有一种方法可以使用特殊方法添加子对象,并且外键ID会自动处理,我很想听到它,因为这会让我更舒服,但如果不是这样,希望对尝试从Asp.Net MVC过渡的其他人有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM