Demon’s Code

PHP

Typo3 MM-relation

by Nox on Apr.02, 2013, under PHP

Recently I had a problem with a typo3 plugin.

I needed to connect some clients with a category and the internet failed me.

TL;DR: The steps to do are:

  • Connect 2 models in the extension builder with a n:m relation and save
  • Open one of the relations with your editor and change it by using the other relations table and adding 'MM_opposite_field' => 'fieldname' to the configuration
I wanted to connect a client-model with a categories-model. So I first created the client model
  • Create a new Model
  • Name it "Client" and set the aggregate root flag to have it build the model data
  • Create another Model
  • Name it "Category" and set the aggregate root flag
  • Now add a relation to Client and name it "categories", connect it to the Category-model
  • Add a relation to Category and name it "clients", connect it to the Client-model
  • Save the extension
  • Open Configuration/TCA/Client.php to see:
    'foreign_table' => 'tx_kssclient_domain_model_category',
    'MM' => 'tx_kssclient_client_category_mm',
  • Open Configuration/TCA/Category.php to see:
    'foreign_table' => 'tx_kssclient_domain_model_client',
    'MM' => 'tx_kssclient_category_client_mm',
  • These lines need to be changed to look like this:
    'foreign_table' => 'tx_kssclient_domain_model_client',
    'MM' => 'tx_kssclient_client_category_mm',
    'MM_opposite_field' => 'categories',
  • The generated code was creating to separate n:m-relations. The changed line will turn the category-client n:m-relation into a client-category m:n-relation, which in turn creates the desired m:m relation
Leave a Comment more...

Mysql & PHP unserialize

by Nox on Jun.01, 2010, under PHP

Yes, I was a bad boy. I did wrong in creating a simple little table that was meant to be an extension for another Table.

Suppose you have an user-table in your database and want to extend it with various data you dont know yet, just as I did some time ago...

You have your table with an id primary key, and some other fields you know, like name, and password. Now you create another table (user_data) to store any, yes ANY, data. This table has an foreign primary key id, that is of course linked to the user.id field. And now, you get 2 more fields user_data.key and user_data.value. Now your table is like an associative array in php, linked only through your id-field to the user table.

Sounds fun, doesnt it?

$user = SELECT * FROM `user` WHERE `id`=X
$user_data = SELECT * FROM `user_data` WHERE `id`=X
$user_data = array_map(convert_with_unserialize, $user_data);

And now you got all the data you want back in PHP. To insert you will have to serialize the data, and to read it, you will have to unserialize it. It's not hard. Actually it's pretty useful, as long as you don't know your data.

But it is hard as hell to run a specific SELECT on user to select everyone that has a white rabbit shoulder tatoo. This data is of course saved in the user_data table as not everyone needs to have a tatoo field.

SELECT `id` FROM `user_data` WHERE `key`='tatoo' AND `value`='s:12:white rabbit';

As you see, you will have to serialize your "question" in some way too. It would be better to write something like this.

SELECT `id` FROM `user_data` WHERE `key`='tatoo' AND UNSERIALIZE(`value`)='white rabbit';

This has some other uses, and actually I used it to correct my mistake in creating such a table in the first place. But for any of you, using some kind of serialized data, this function might help.

BEGIN
	DECLARE len INT DEFAULT LENGTH(str);
	DECLARE pos INT DEFAULT 0;

	IF ISNULL(str) THEN
		RETURN NULL;
	END IF;

	IF LEFT(str, 1) = 's' THEN
		SET pos = LOCATE(':', str, 3) + 2;
		RETURN MID(str, pos, len - pos - 1);
	END IF;

	IF LEFT(str, 1) = 'a' THEN
		RETURN str;
	END IF;

	RETURN MID(str, 3, len - 3);
END

It checks for null first, then unserializes strings, then returns arrays as themselfs and then guesses that there are only numbers left. Of course everything is still returned as string, but PHP should do the rest of the dirty work.

Have fun

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...