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

[eluser]OverZealous[/eluser]
After apparently enough caffeine has gotten into my system, I finally found out that the problem IS within DataMapper / DMZ, and, yes, it can be fixed.

Here's the temporary solution: search for and replace 'left' with 'left outer' in the libraries/datamapper.php file.

I'm planning on implementing this anyway, as that is more appropriate syntax (they are synonymous with each other). It should be fully supported across all databases, since LEFT OUTER JOIN is the correct SQL term.

[eluser]mrtavo[/eluser]
[quote author="OverZealous.com" date="1247528895"]After apparently enough caffeine has gotten into my system, I finally found out that the problem IS within DataMapper / DMZ, and, yes, it can be fixed.

Here's the temporary solution: search for and replace 'left' with 'left outer' in the libraries/datamapper.php file.

I'm planning on implementing this anyway, as that is more appropriate syntax (they are synonymous with each other). It should be fully supported across all databases, since LEFT OUTER JOIN is the correct SQL term.[/quote]


Thanks for the fast answer, I will try that soon.

[eluser]PoetaWD[/eluser]
I think I just got me a answer:

Quote:Adding a Global Extension
// In DataMapper Config
$config['extensions'] = array('json'); // Include the json extension

If that doesnt work I will post here again...

Quote:Hey man !

I havent being around because I am at the beach right now,,,

Have you received my Post Card ?

Hehe... I will try to send you another... from the city I am right now... :-P

I have a question regarding the JSON included extension ... I dont know how to use it.. :S

I followed the instructions on the manual but it gave me a ERROR:

Code:
Fatal error: Uncaught exception 'Exception' with message 'Unable to call the method "set_json_content_type" on the class Area' in C:\wamp\www\SGP\system\application\libraries\datamapper.php:587 Stack trace: #0 [internal function]: DataMapper->__call('set_json_conten...', Array) #1 C:\wamp\www\SGP\system\application\controllers\careas.php(145): Area->set_json_content_type() #2 [internal function]: Careas->js_list() #3 C:\wamp\www\SGP\system\codeigniter\CodeIgniter.php(232): call_user_func_array(Array, Array) #4 C:\wamp\www\SGP\index.php(115): require_once('C:\wamp\www\SGP...') #5 {main} thrown in C:\wamp\www\SGP\system\application\libraries\datamapper.php on line 587

Here is my code... I used my own code to create the json output.. but I think that your way is better... Big Grin .. and I want to learn it.

Code:
function js_list()
    {
        
        $this->output->enable_profiler(FALSE);
        
        //Inicia um novo tipo de Objeto
        $obj = new Area();
        
        $obj->set_json_content_type();
        echo $obj->to_json(array('id', 'stNome'), TRUE);

        
        /*$obj->get();
        
        //Passa a variavel $objetos para o arquivo view como sendo TODOS os itens do tipo de objeto  
        $objetos = $obj->all;
        
        $rows = 0;
    
        foreach($objetos as $row)
        {
            $item = Array("Id" => $row->id,
                          "stNome" => $row->stNome);
    
            $itemList[] = $item;
            
            $rows = $rows + 1;
        }
    
        //$rows = $objetos->num_rows();
        $data = json_encode($itemList);
    
        echo '({"total":"' . $rows . '","results":' . $data . '})';*/
    }

Thank you man...

[eluser]OverZealous[/eluser]
@PoetaWD:
Did you enable the extension? You need to add it to either the config/datamapper.php config file, by adding it to the array under $config['extensions'], or by adding it to the Area model's $extensions variable.

If you did add it, let me know where you added it. I'm slowly working on building a better set of example code that I can test new features on, so I can't discount that it is a bug.

I haven't made it to my P.O. Box since returning from vacation, but I'll check this week!

[eluser]PoetaWD[/eluser]
[quote author="OverZealous.com" date="1247539730"]@PoetaWD:
Did you enable the extension? You need to add it to either the config/datamapper.php config file, by adding it to the array under $config['extensions'], or by adding it to the Area model's $extensions variable.

If you did add it, let me know where you added it. I'm slowly working on building a better set of example code that I can test new features on, so I can't discount that it is a bug.

I haven't made it to my P.O. Box since returning from vacation, but I'll check this week![/quote]

LoL

My bad man... forgot to load the extension...

If you need any help testing or.. I can do anything... just ask me !

Now for the code:

I loaded the extension but it does not show the data in the JSON string...

It gives me:

Code:
{ "id": null, "stNome": null }

Code:
$obj = new Area();
    
$obj->set_json_content_type();

echo $obj->to_json(array('id', 'stNome'), TRUE);

I tryed:

Code:
$obj = new Area();
        
$obj->get();
        
$objects = $obj->all;
        
$objects->set_json_content_type();

echo $objects->to_json(array('id', 'stNome'), TRUE);

But it gives me a error... (I dont have it because I am at a public computer, my notebook is at home, I am the beach...)

Have you ever used EXT JS? I am trying to learn it... it is really cool.. that is why I need the json code.

They have this tutorial to build a data GRID with CI + EXT :
http://extjs.com/learn/Tutorial:ExtJs_CodeIgniter

But it is kind of hard and complicated

I want to post a tutorial like: How to build a GRID with CI + EXT in 5 min using DMZ

It is so easy when you are using DMZ ... Tongue

But I want to know how to use the JSON extension to make the tutorial,

I think that many other people will start using DMZ after they see a tutorial like this!

Thanks for the help

[eluser]OverZealous[/eluser]
My examples are horrible, I just realized. Doh!

You see NULLs because Area didn't load any data.

Use it like this:
Code:
$a = new Area();
$a->get(); // or whatever
echo $a->to_json(array('id', 'stNome'), TRUE);

Also, the next update of DMZ includes an additional method for the json extension: all_to_json, which converts all of the results in ->all into a JSON array. I've also included an extension for converting to and from associative arrays.

[eluser]PoetaWD[/eluser]
[quote author="OverZealous.com" date="1247544776"]My examples are horrible, I just realized. Doh!

You see NULLs because Area didn't load any data.

Use it like this:
Code:
$a = new Area();
$a->get(); // or whatever
echo $a->to_json(array('id', 'stNome'), TRUE);

Also, the next update of DMZ includes an additional method for the json extension: all_to_json, which converts all of the results in ->all into a JSON array. I've also included an extension for converting to and from associative arrays.[/quote]

They are not horrible... just miss something... Tongue

So... the to_json() would not convert all the data to JSON ? LOL

So I will keep using my bad code to convert the data while you do not release the new JSON extension:

Code:
$a = new Area();
$a->get();

$objects = $a->all;

foreach($objects as $row)
        {
            $item = Array("Id" => $row->id,
                          "stName" => $row->stName);
    
            $itemList[] = $item;
            
            $rows = $rows + 1;
        }

        $data = json_encode($itemList);
    
        echo '({"total":"' . $rows . '","results":' . $data . '})';

SUGESTION: Can you add a option to include the total number of objects being written in the json ? Just like in my example? The "total".

See ya

[eluser]OverZealous[/eluser]
Your code is fine (although, you could place the $itemList and $rows in an associative array and json_encode them all at once).

I won't be adding the total or options like that for now. One reason is that different libraries expect different results. For example, the result set for a Dojo item store (which I use in my code) looks like this:
Code:
{
    'identifier': 'id',
    'label': 'name',
    'items': [ ... ],
    'numRows': nn
}

The second reason is that, as in your example, it is easy to concatenate JSON data.

The third reason is that total or numRows should usually require a separate query. If you are bothering to use a data grid, the assumption is that there are a lot of rows, and you are paging the results (at least, I hope so).

My code for returning my grid results usually looks something like this (pseudo-code):
Code:
// Create $object somewhere

// Configure the query parameters
$object->where(...);

// Clone before querying for total number of rows
$summary = $object->get_clone();
$total = $summary->count(); // total number of items that match the query

// look up the current page requested
// $start and $page_size come from the client
$object->get($start, $page_size);

// Convert to JSON-compatible result
$response = array();
$response['total'] = $total;
$response['result'] = $object->all_to_array(); // Not available until next release

// send JSON
echo(json_encode($response));

Finally, the next release of DMZ includes the previously mentioned associative array extension, so you can get the data in a form that is easy to convert to JSON if you need to enhance it.

[eluser]mcnux[/eluser]
[quote author="OverZealous.com" date="1247290063"]mcnux

There are two solutions to your problem.

1) Since you are remapping the client to a different user, the easiest solution is to simply save the clients to the new user. Because each client can only be related to one user (both by rule AND by database design), the new $userTwo->id will be set in $client->user_id. You will still need to load the new Clients on the new User ($userTwo->client->get()) to see the update.
[/quote]

I thought saving the clients to the new user is what I was trying to do in the following?

Code:
$userOne = new User();
$userOne->get_by_id($id);

// $userTwo = new User(); // whoops, this should show loading another user, see next line
$userTwo = new User($idTwo);
$userTwo->save($userOne->client->get()->all);

But this does not have any affect - the user_id remains that of $userOne. If there is another method I should be using, please advise.

Quote:Also, the old user will still have the related set in memory for this HTTP request (meaning, $userOne->client->all will still contain a reference). There is no way for DM to know that you are moving clients from one user to another, due to PHP garbage collection limitations.

Yes I am aware of this, and am checking the database directly.

Quote:2) $userOne->delete($client) requires that $client->user_id be able to be stored as NULL. When you set FK rules in most databases, you often need to explicitly allow NULLs, as is mentioned in the docs (under Rules). Please read over your database's docs, and make sure that your rules are not preventing the column from being set as NULL.

Yes I have set the user_id field on clients to allow NULLs. I am able to remove the relationship (set the user_id to NULL) when I delete $userOne, the problem is I don't want to delete the user, I just want to reallocate his clients to another user.

You seem to think I am missing something and this should be working, so hopefully I am doing something stupid. I would appreciate an example of achieving your first solution as I thought I was trying exactly what you suggested Sad

[eluser]OverZealous[/eluser]
@mcnux
The code you have written should work. Because it does not, it means that the issue is likely related to your database configuration, or other configuration issues.

• Have you checked to see if you are getting a TRUE result from your save? I see this a lot, and it is 100% wrong:
Code:
$userTwo->save($userOne->client->get()->all);

It should always be encapsulated in an if statement:
Code:
if($userTwo->save($userOne->client->get()->all)) {
    // success
} else {
    show_error('Error Saving: ' . $userTwo->error->string);
}

Otherwise, you don't know that there wasn't a validation error. Note: error->string may still be empty, if your models are mis-configured.

• Try removing all FK restraints.

• Is $userTwo a new user, or an existing one? If it is new, is the problem related to the database FK rules preventing the column from being changed until $userTwo is saved in a separate transaction?

• Have you enabled profiling? Call $this->output->enable_profiler(TRUE); in your controller. Look through the queries being made, make sure they make sense. You should see queries for each save of each client, including checking to see if the relationship exists, and changing it if necessary.

• Change your CI logging to DEBUG, and look through the log when you save.

Spend a little more time looking into it, I'm sure you'll find what's wrong! ;-)




Theme © iAndrew 2016 - Forum software by © MyBB