Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.1
#51

[eluser]matyhaty[/eluser]
For those who are interested.
Ive been trying to do the advanced relationships for this, which would help the searching (I think....)
I have failed pretty badly!!

Attached is a very basic implementation... (with SQL)
http://s3d.co.uk/client_sites/DMTest.zip

I must be doing something obviously wrong - or it could be my whole logic!

Im on the verge for storing ID's in the qualification table, but it would be such a shame not to use the ORM properly
Any help welcomed, advise, abuse or otherwise!!!

regards

EDIT:
Ive updated the save part to the below - still doesnt work, but more on the right lines ithink ??
Code:
public function index()
    {

        $p = new Person(1);
        $q = new Qualification(1);
        $qt = new Qualificationtype(8);
        $p->save(array(
            'qualificationtype' => $qt,
            'qualification' => $q,
            
        ));
        $p->check_last_query();
        
        $this->load->view('welcome_message');

    }
#52

[eluser]Damir Sivic[/eluser]
Am I Doing This Right? there's so many "select ..." and "update ..." in debug report.

Relationship many to many campaigns and categories with one join field, and I want to save data into relationship table
Code:
$c = new Camapign();
$c->load_extension('array');
$c->from_array($_POST);

$cat = new Category();

// this is join field
//$this->input->post("iznos")

if ($c->save($cat->all)) {
    foreach($this->input->post("iznos") as $k => $v){
        $cat->get_by_id($k);
        $c->set_join_field($cat, 'iznos', $v);
    }
}
#53

[eluser]matyhaty[/eluser]
Back to my qualifications problem!!!

Attached is basic image which shows the flow.....
With this setup I can quite easily access all the information for a person's qualifications....

Code:
$p = new Person(2);
$p->qualifications->get();
$p->qualifications->include_related('qualificationtype', '*', TRUE, TRUE)->get();

foreach($p->qualifications as $quallys)
{
echo $quallys->qualificationtype->title;  // the type of qualification
etc... etc....
}

The issue Im having is searching person for particular types of qualifications.....

Story Board:
1) Get all persons in the system which have qualification type '6'
2) Get all persons in the system which have qualification type 6 AND 4
3) Get all persons in the system which have qualification type 6 OR 4

The previous posts try and do this, but can only do 1 and 3......
#54

[eluser]WanWizard[/eluser]
Code:
$p
    ->where_related('qualifications/qualificationtype', 'type', 4)
    ->where_related('qualifications/qualificationtype', 'type', 6)
    ->get();
You can define these in a loop if need be.
#55

[eluser]dejavu[/eluser]
<edit> bad question - I've been spoiled by using a similar ORM with slightly different behavior.
#56

[eluser]Damir Sivic[/eluser]
just yes/no would be ok Smile
#57

[eluser]WanWizard[/eluser]
[quote author="dejavu" date="1308209472"]<edit> bad question - I've been spoiled by using a similar ORM with slightly different behavior.[/quote]
What kind of behaviour? There's always room for improvement, if you have a suggestion...
#58

[eluser]Flyingbeaver[/eluser]
Hi folks,


Reading the doc I have tried to use : Many-to-Many Reciprocal Self Relationships...

Datamapper 1.8.1

With the following code :

Model

Code:
class Service extends DataMapper {

    var $table = 'services';

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------

    var $has_one = array();

        var $has_many = array(
            'related_service' => array(
                'class' => 'service',
                'other_field' => 'service',
                'reciprocal' => TRUE
        ),
            'service' => array(
                'other_field' => 'related_service',
                'reciprocal' => TRUE
        ),

    );



I have two tables :

services and services_services

services_services has 3 fields : id, service_id, related_service_id.


Controller

Code:
$service = new Service();
$service->include_related('related_service', array('id', 'title', 'url'), TRUE, TRUE);
$service = $service->get()->all_to_array();


I have the following error :

Error Number: 1146
Table 'test_db.related_services_services' doesn't exist

If I change table name I got no errors but fields values are NULL.



Thx for your help !
#59

[eluser]Flyingbeaver[/eluser]
Ok i have the solution :

I have changed my get to array

Code:
$service = $service->get()->all_to_array(
                array(
                    'id',
                    'title',
                    'url',
                    'related_service_id',
                    'related_service_url',
                )
            );


Thx dan for your great work, I can't wait to try fuel and see your work on this Wink
#60

[eluser]Anestetikas[/eluser]
Found a new problem to deal with.
I have a DB with folowing tables funds -< inventories -< files -< documents
All of these 4 tables has a many to many relations to languages and characteristics tables.

There are like 2k rows in funds 5k in inventories 4mil in files and ~200k in documents.
Every one of these rows has 0 1 or 2 relations to languages and characteristics.

I want to get all related languages and characteristics to specific fund, inventory, file.

If I do or_where_related:
Code:
$kalbos = new Language;
                $kalbos->select('description');
                $kalbos->or_where_related('inventory/fund', 'id', $fondas->id);
                $kalbos->or_where_related('file/inventory/fund', 'id', $fondas->id);
                $kalbos->or_where_related('document/file/inventory/fund', 'id', $fondas->id);
                $kalbos->group_by('id')->get();
                $this->data['kalbos'] = $kalbos;
it does a huge query:
Code:
SELECT  `languages`.`description`
FROM (
`languages`
)
LEFT OUTER JOIN  `inventories_languages` inventories_languages ON  `languages`.`id` =  `inventories_languages`.`language_id`
LEFT OUTER JOIN  `inventories` inventories ON  `inventories`.`id` =  `inventories_languages`.`inventory_id`
LEFT OUTER JOIN  `files_languages` files_languages ON  `languages`.`id` =  `files_languages`.`language_id`
LEFT OUTER JOIN  `files` files ON  `files`.`id` =  `files_languages`.`file_id`
LEFT OUTER JOIN  `inventories` file_inventories ON  `file_inventories`.`id` =  `files`.`inventory_id`
LEFT OUTER JOIN  `documents_languages` documents_languages ON  `languages`.`id` =  `documents_languages`.`language_id`
LEFT OUTER JOIN  `documents` documents ON  `documents`.`id` =  `documents_languages`.`document_id`
LEFT OUTER JOIN  `files` document_files ON  `document_files`.`id` =  `documents`.`file_id`
LEFT OUTER JOIN  `inventories` document_file_inventories ON  `document_file_inventories`.`id` =  `document_files`.`inventory_id`
WHERE  `inventories`.`fund_id` =831
OR  `file_inventories`.`fund_id` =831
OR  `document_file_inventories`.`fund_id` =831
GROUP BY  `languages`.`id`
EXPLAIN:
Code:
+----+-------------+---------------------------+--------+------------------------------+------------------+---------+----------------------------------------------+------+-------------+
| id | select_type | table                     | type   | possible_keys                | key              | key_len | ref                                          | rows | Extra       |
+----+-------------+---------------------------+--------+------------------------------+------------------+---------+----------------------------------------------+------+-------------+
|  1 | SIMPLE      | languages                 | index  | NULL                         | PRIMARY          | 2       | NULL                                         |    1 |             |
|  1 | SIMPLE      | inventories_languages     | ref    | lan_inv_unique,language_id   | lan_inv_unique   | 2       | pagrindas.languages.id                       |   59 | Using index |
|  1 | SIMPLE      | inventories               | eq_ref | PRIMARY                      | PRIMARY          | 2       | pagrindas.inventories_languages.inventory_id |    1 |             |
|  1 | SIMPLE      | files_languages           | ref    | lang_file_unique,language_id | lang_file_unique | 3       | pagrindas.languages.id                       |    1 | Using index |
|  1 | SIMPLE      | files                     | eq_ref | PRIMARY                      | PRIMARY          | 4       | pagrindas.files_languages.file_id            |    1 |             |
|  1 | SIMPLE      | file_inventories          | eq_ref | PRIMARY                      | PRIMARY          | 2       | pagrindas.files.inventory_id                 |    1 |             |
|  1 | SIMPLE      | documents_languages       | ref    | lang_doc_unique,language_id  | lang_doc_unique  | 3       | pagrindas.languages.id                       |  525 | Using index |
|  1 | SIMPLE      | documents                 | eq_ref | PRIMARY                      | PRIMARY          | 4       | pagrindas.documents_languages.document_id    |    1 |             |
|  1 | SIMPLE      | document_files            | eq_ref | PRIMARY                      | PRIMARY          | 4       | pagrindas.documents.file_id                  |    1 |             |
|  1 | SIMPLE      | document_file_inventories | eq_ref | PRIMARY                      | PRIMARY          | 2       | pagrindas.document_files.inventory_id        |    1 | Using where |
+----+-------------+---------------------------+--------+------------------------------+------------------+---------+----------------------------------------------+------+-------------+
From EXPLAIN we can see that it creates 3 temp tables document_files, document_file_inventories and file_inventories.
Query execution:
Code:
34 rows in set (24 min 18.99 sec)

Is there a way to get related languages for each object (its very fast), and then merge them into one $languages object?

EDIT:
Went around with getting ids all_to_array for each object, then merged ids to $langIDs array, then selected once again by where_in('id', $langIDs); Lots of code, but seems to get proper stuff in 0.04sec instead of 25minutes.




Theme © iAndrew 2016 - Forum software by © MyBB