Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

associate

posted by mungiu 8 years ago

Please Bond give an idea how to aproach this.

Sql

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL auto_increment,
  `category` varchar(100) NOT NULL,
  `pid` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `pid` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `categories`
--


--
-- Constraints for dumped tables
--

--
-- Constraints for table `categories`
--
ALTER TABLE `categories`
  ADD CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `categories` (`id`);

array:

Array
(
    [0] => Array
        (
            [Items for Sale] => Array
                (
                    [0] => Array
                        (
                            [Antiques & Collectibles] => Array
                                (
                                    [0] => Advertising
                                    [1] => Animation & Cartoons
                                    [2] => Appliances
                                    [3] => Autographs
                                    [4] => Autos & Vehicle Memorabilia
                                    [5] => Cigarette & Tobacco Paraphernalia »

                                    [6] => Clocks »
                                    [7] => Coins & Currency
                                    [8] => Comics »
                                    [9] => Dolls »
                                    [10] => Entertainment Memorabilia »
                                    [11] => Food & Beverage
                                    [12] => Furniture
                                    [13] => Groups & Cultures
etc...

i want this array to insert in that table to have this categories recurisively. i try something like this

foreach ($categories100 as $key => $value) {
            foreach ($value as $key1 => $value1) {
                $categorii = new Category();
                $categorii->setCategory($key1);
                foreach ($value1 as $key2 => $value2) {
                    foreach ($value2 as $key3 => $value3) {
                        $subcategorii = new Category();
                        $categorii->associateCategory($categorii);
                        $subcategorii->setCategory($key2);
                    }
                }
//print_r($categorii);
            }
        }

but it says that: The table categories is not in a *-to-manyrelationship with the table categories. but how u say i setted foreig key reference itself.

Array
(
    [one-to-one] => Array
        (
        )
  
    [many-to-one] => Array
        (
            [0] => Array
                (
                    [table] => categories
                    [column] => pid
                    [related_table] => categories
                    [related_column] => id
                )
        
        )
  
    [one-to-many] => Array
        (
            [0] => Array
                (
                    [table] => categories
                    [column] => id
                    [related_table] => categories
                    [related_column] => pid
                    [on_delete] => no_action
                    [on_update] => no_action
                )
        
        )
  
    [many-to-many] => Array
        (
        )
  
)

Can you please post a the backtrace and error message from the exception that says categories is not related to categories?

posted by wbond 8 years ago

Perhaps I'm reading this wrong... but if you were to associate a single Category, wouldn't you want to be doing it on the sub category? NOT the parent category?

                    foreach ($value2 as $key3 => $value3) {
                        $subcategorii = new Category();
                        $subcategorii->associateCategory($categorii);
                        $subcategorii->setCategory($key2);
                    }

Not sure if this will fix the complain, but it only seems logical.

posted by mattsah 8 years ago

Sorry but i can't understand what u want to say, here i try to make a tree and i initiate a new category and the i try to relate to upper category.

posted by mungiu 8 years ago

EDIT: The suggestion on line 8 (as far as I can tell) is bunk... it doesn't appear as though you can associate in that way. The confusing part about self-referencing tables is that from the child's perspective, it is related only to one parent... from the parent's perspective it has many children. But the associate method, in my understanding, is based on the *-to-many relationship, as such, the expected naming would be ->associateCategories() -- in which case, the required argument would have to be a list of categories. The recursive function I offered should hypothetically still work to my knowledge.

Line 8 of your PHP code above is currently:

$categorii->associateCategory($categorii);

I'm saying I think it needs to be:

$subcategorii->associateCategory($categorii);

That being said, you may also want to think about using a recursive function.. Your ids are auto incremented so they do not need to match anything in particular. From what I can tell you are just trying to make sub categories a child of the parents based on the array keys:

function buildFromArray($product_array)
{

     $child_categories = array();

     foreach ($product_array as $key => $value) {

          $child_categories[] = $category = new Category();

          if (is_array($value)) {
               $category->setCategory(fHTML::decode($key));
               $category->associateCategories(buildFromArray($value, $category));
          } else {
               $category->setCategory(fHTML::decode($value));
          }

          $category->store();
     }

     return $child_categories;
}

I have not tested this but in theory that function should work for what I believe you are trying to do. The only weird thing is that all your children will be stored before the parents. But I think this is a necessity of associate. I could be wrong... Will, are you able to associate records which have not been stored yet?

posted by mattsah 8 years ago

tank u for sugestion i will try your function. i did't in way like this bu i was currios how that can be doing with association.

//        foreach ($categories100 as $key => $value) {
//            foreach ($value as $key1 => $value1) {
//                $categorii = new Category();
//                $categorii->setCategory($key1);
//                $categorii->store();
//                foreach ($value1 as $key2 => $value2) {
//                    foreach ($value2 as $key3 => $value3) {
//                        $subcategorii = new Category();
//                        $subcategorii->setCategory($key3);
//                        $subcategorii->setPid($categorii->getId());
//                        $subcategorii->store();
//                        foreach ($value3 as $key4 => $value4) {
//                            //foreach ($value4 as $key5 => $value5) {echo $value5;
//                            $subsubcategorii = new Category();
//                            $subsubcategorii->setCategory($value4);
//                            $subsubcategorii->setPid($subcategorii->getId());
//                            $subsubcategorii->store();
//                            //}
//                        }
//                    }
//                }
posted by mungiu 8 years ago