Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]qureshi[/eluser]
Hi OverZealous,

Categories aren't shared, but the structure of the trees can be the same for two users. Meaning that there is a possibility for both user X and Y to have two private categories each, one parent category called science and one subcategory called Biology. Theoretically they might both decide to place a third category "Evolution" under their respective Biology categories. All of user A's categories are independant of users B's categories.

I've allowed for an unlimited number of subcategories.

When a user tries to create a category, I want to first check if that user already has an identical category in a given parent category (this parent category also belonging to him.) How do I do this?

Nadeem Qureshi

[eluser]OverZealous[/eluser]
I'm getting confused why it matters if they have the same parent category or not. Are the parent categories shared?

I think you are looking stuff up by 'name' when you should be looking them up by 'id'. If you looked them up by 'id', then you wouldn't have to do these crazy joins.

For example, say your web form looked like this:
Code:
Parent Category: [ Science       |V]
New Subcategory: [ Biology         ]

The parent category would be a select box with all of the current main categories for the user. Then you would have the absolute id, and you could check like this:
Code:
$parent = new Category();
$parent->get_by_id($this->input->post('parent_id'));
$sub = $parent->subcategory->where('name', $this->input->post('name'))->get();
if($sub->exists()) {
    // handle existing category
} else {
    // handle new category
}

Alternatively, if you prefer to let the user input the parent name manually, then you would write almost the exact same code, but replace $parent with $user, and subcategory with $category.

As long as users don't share categories, I'm not sure why it matters if another user has the same category name as another user.

[eluser]qureshi[/eluser]
Hi,

Quote:I’m getting confused why it matters if they have the same parent category or not. Are the parent categories shared?

No, nothing is shared.

Let me try to explain myself better.

User A categories:

-- Science
-----------Biology

User B categories:

-- Science
-----------Biology
-------------------Videos
-------------------Evolution

The categories under User B are totally separate from the categories under User A. What User B puts under his Science category does not affect the Science category of User A.

Consider these scenarios:

1) User A wishes to create another "Science" category that has no parents.
Desired outcome: He should not be allowed to, because he already has such a category.

2) User A wishes to create his own Videos category without parents.
Desired outcome: He should be allowed, since he has no such category of his own.

3) User A wishes to create another Videos category under his Biology category, giving him two separate and independant Videos categories.
Desired outcome: He should be allowed to, because there is no Videos category under Biology yet.

4) User A tries to repeat scenario 3.
Desired outcome: He should not be allowed to, since there already exists an Video category under his Biology category.

Take scenario 3. If I merely check to see whether or not some category named Biology has a subcategory called Videos, then I risk a false positive because there does exists a category named Biology with a subcategory called Videos, BUT they belong to user B. I only want to check if this is true or not for User A.

So, I need some code that can handle the above scenarios.

The parent category name is provided by the user as of now, but I think your idea of having a select box is better.

There is no subcategory model. Each category row has a parent field. That's how I did it.

I hope I was clearer this time. Thanks for helping :-)

Nadeem

[eluser]bEz[/eluser]
@qureshi
Have you setup the Category Model/Table using Self-Referencing to accommodate the use of Sub-Categories?

[eluser]PoetaWD[/eluser]
@qureshi


Tongue

Take a look at this... it REALLY helped me figure out how to do it...

http://dev.mysql.com/tech-resources/arti...-data.html

I didnt used it to make a code for it using the DM,,, made it using RAW php... just for fun...

Basically the table for it would be:

Code:
CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);

Put some values for the table:

Code:
INSERT INTO category
VALUES(1,'ELECTRONICS',NULL),(2,'TELEVISIONS',1),(3,'TUBE',2),
(4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),
(7,'MP3 PLAYERS',6),(8,'FLASH',7),
(9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6);

Code:
+-------------+----------------------+--------+
| category_id | name                 | parent |
+-------------+----------------------+--------+
|           1 | ELECTRONICS          |   NULL |
|           2 | TELEVISIONS          |      1 |
|           3 | TUBE                 |      2 |
|           4 | LCD                  |      2 |
|           5 | PLASMA               |      2 |
|           6 | PORTABLE ELECTRONICS |      1 |
|           7 | MP3 PLAYERS          |      6 |
|           8 | FLASH                |      7 |
|           9 | CD PLAYERS           |      6 |
|          10 | 2 WAY RADIOS         |      6 |
+-------------+----------------------+--------+

Than you can play with the queries:

To get the full tree:
Code:
+-------------+----------------------+--------------+-------+
| lev1        | lev2                 | lev3         | lev4  |
+-------------+----------------------+--------------+-------+
| ELECTRONICS | TELEVISIONS          | TUBE         | NULL  |
| ELECTRONICS | TELEVISIONS          | LCD          | NULL  |
| ELECTRONICS | TELEVISIONS          | PLASMA       | NULL  |
| ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS  | FLASH |
| ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS   | NULL  |
| ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL  |
+-------------+----------------------+--------------+-------+

Code:
SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.name = 'ELECTRONICS';

Take a look at the article...

I hope it helps...

[eluser]naren_nag[/eluser]
Hi,

I love the auto-timestamping of the created and updated field. Now here's the problem: my server is in the US, my clients are in India. So the timestamps have an 11 hour lag. I could update time stamps on my own, but am I missing something? Is there a more elegant way to make this happen.

cheers,

Naren

[eluser]PoetaWD[/eluser]
[quote author="naren_nag" date="1246190022"]Hi,

I love the auto-timestamping of the created and updated field. Now here's the problem: my server is in the US, my clients are in India. So the timestamps have an 11 hour lag. I could update time stamps on my own, but am I missing something? Is there a more elegant way to make this happen.

cheers,

Naren[/quote]

You can change the timezone in apache in order to work...

To change the time:

Open your .htaccess file and add the following line:

SetEnv TZ THE_LOCATION

where THE_LOCATION is the specific timezone you want to define.

A list of Supported Timezones can be found here:
http://www.php.net/manual/en/timezones.php

Big Grin

I am at brazil, so my .htaccess would be like this:

Code:
SetEnv TZ "America/Sao_Paulo"

for India it would be like this:

Code:
SetEnv TZ "Asia/Calcutta"

[eluser]OverZealous[/eluser]
Wow, lots of updates in less than a day...

@qureshi
The simple answer to #3 is this:
Code:
$c = new Category();
// check against logged in user
$c->where_related($user);
// check against parent category (you should already have looked this up)
$c->where_related_parent($parent_category);
// check against name
$c->where('name', $name);
$c->get();
if($c->exists()) {
    // deny or show existing
} else {
    // create new
}

Now, if, like I mentioned, parent categories are NOT shared, then you can skip the check against the logged in user (for subcategories), because the parent category check will already have prevented a false positive.

@naren_nag
I never mentioned this in the docs, but DMZ also stores the timezone when saving timestamps. I probably should make this configurable.

If this has changed from when you used the original DataMapper, you can find-and-replace this 'Y-m-d H:iConfused O' with this 'Y-m-d H:iConfused' within libraries/datamapper.php.

Otherwise, another method than what PoetaWD mentioned is to change the TimeZone within PHP itself, using date_default_timezone_set, which also gives you the option of changing the timezone for each user separately.

Time in PHP is probably the area it lags the most for me, between the limited timezone configuration, poor method set for manipulating time and intervals, and other issues. There's a new DateTime module coming, but it probably won't be usable until PHP 6.0 or later.

[eluser]OverZealous[/eluser]
DataMapper OverZealous Edition Update

Quote:Version 1.3.2: Important Bugfix:

PoetaWD found a bug that prevented using include_related from working correctly when using multiple relationships to the same model.

I also added a check_last_query method to quickly print out the last query, formatted, and placed within <pre> tags.
Learn more about check_last_query here

Also, you might find your name on the credits page. I went through the forums and added anyone who has found a bug. Let me know if I missed anyone ;-)

Quote:Download the Latest Version Here

View the change log and the upgrade process
Having issues? Please look through the Troubleshooting Guide & FAQs
View the Complete Manual

[eluser]PoetaWD[/eluser]
I´m glad I helped !

And.. be sure I will keep posting any issues I have to make this perfect !

I hope one day more people will see how EASY and FUN it is to make programs with CI + DMZ !

Edit: Just tested it in my app and it worked ! Thanks again man !




Theme © iAndrew 2016 - Forum software by © MyBB