CodeIgniter Forums
How to call a Stored Procedure using DataMapper ORM? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to call a Stored Procedure using DataMapper ORM? (/showthread.php?tid=56095)



How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-26-2012

[eluser]stoan09[/eluser]
Is it possible to call a MySQL Stored Procedure using DataMapper ORM, I can't find this on the Documentation, I have be goggling as well but nothing. Thanx


How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-26-2012

[eluser]WanWizard[/eluser]
What exactly do you want to achieve?

Datamapper is an ORM, which uses models to provide an object representation of records in a table. It's not made to run arbitrary queries.

If your stored procedure returns full records of a table, so it's result can be mapped to a model, you could add a method to the model that calls the stored procedure using standard DB calls. You can then call _process_query(), passing the CI_DB_Result object from your custom query, to convert the result into Datamapper objects.

Note that the resultset MUST include the 'id' column.


How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-26-2012

[eluser]stoan09[/eluser]
I'm building a location based events listing app. I store events on the db with coordinates,(latitude and longitude), so im looking to write a stored procedure that will take advantage of MySQL's geometry features. So the stored procedure would accept latitude and longitude ans distance as parameters and return events within the specified distance. The events table has the field "id"

Thank you


How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-26-2012

[eluser]WanWizard[/eluser]
Then it should be possible using the method I described.


How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-26-2012

[eluser]stoan09[/eluser]
I haven't started to write my code but If I understand what you described above, it would go something like this

$e = new Event();

$result = $e->query("call my_stored_proc_name(my_parameters)");



How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-27-2012

[eluser]WanWizard[/eluser]
No, what I meant was something like

Code:
class Event extends Datamapper
{
    public function mymethod()
    {
        $result = $this->db->query('call my_stored_proc_name(my_parameters)');
        $this->_process_query($result);
        return $this;
    }
}



How to call a Stored Procedure using DataMapper ORM? - El Forum - 11-27-2012

[eluser]stoan09[/eluser]
Thank you, Making sense so far. I think it will make more sense once I have started creating my objects. I like to separate my class that defines my object with the one that contains methods for the object. As my stored procedure will return more than one event, I assume this line will return a list or array of objects?

<code>
return $this;
</code>