• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call functions in to model..

#1
[eluser]tnathos[/eluser]
Hi, i have the next problem..

Y have a model whit several functions, i liked use code the one funcion in other function for reuse code in the same model and not rewrite the same code in varios funcionts..but i cant.. any know i can, CI permit load or call functions in the same model? if no how make this?

example.

Code:
<?php
class CommonModel extends Model {
    
    function CommonModel()
    {
        parent::Model();
    }    
    
    function 1(){
        a + b = c}

    function 2(){
        n * d + f = x
                function 1 + x...
        }

    
?>

    
}

#2
[eluser]Dam1an[/eluser]
I'm not quite sure what you're after :S
Do you want a common model which you can extend and then use those methods in all your models?
Or a llibrray with common functionality which you can call from your models?

:S

#3
[eluser]Yorick Peterse[/eluser]
[quote author="Dam1an" date="1241355388"]I'm not quite sure what you're after :S
Do you want a common model which you can extend and then use those methods in all your models?
Or a llibrray with common functionality which you can call from your models?

:S[/quote]

I think he wants to load a function that's stored into another function.

Code:
<?php
//Call the first function
$this->some_model->function1();

//Now we can call the second function
$this->some_model->function2();

?>

#4
[eluser]tnathos[/eluser]
i want use a function in the same model for other function... for reuse code and not rewrite the same code in severals funcionts.. for example, in this code.. i have 3 functions diferents but whit a same code for insert data in DB... this code its the same, but run in diferents event in my APP... its no call funcions bettewen a controller else into same model for reuse code.

<?php
MODEL....

FUNCTION A {
insert into table a field a, b parametres -> post xxxxx
delete * table b
}

FUNCTION B {
insert into table a field a, b. parametres -> post xxxxx
delete * table c
}
?>

I THINK WHAT CAN BE....

<?php

MODEL

FUNCTION A {
CALL FUNCTION C
delete * table b
}

FUNCTION B {
CALL FUNCTION C
delete * table a
}

FUNCTION C {
insert into table a field a, b. parametres -> post xxxxx
}

?>

#5
[eluser]Dr. Seuss[/eluser]
Yorick's post is almost your answer (I once had the same question). You use the "$this->functionname()" to call a function from a function that is defined WITHIN the same model / class.


<?php

MODEL

FUNCTION A {
$this->FUNCTION C
delete * table b
}

FUNCTION B {
$this->FUNCTION C
delete * table a
}

FUNCTION C {
insert into table a field a, b. parametres -> post xxxxx
}

?>

#6
[eluser]tnathos[/eluser]
No works it... generate the next error..

Call to undefined method ClientModel::restTrans() in application\models\clientModel.php on line 118

code...

Code:
<?php
class ClientModel extends Model {
    
    function ClientModel()
    {
        parent::Model();
    }

function newTrasac()
    {
    $this->restTrans;
        }

function restTrans (){
        echo "anything...";
        }

?>

#7
[eluser]Yorick Peterse[/eluser]
[quote author="Dr. Seuss" date="1241388387"]Yorick's post is almost your answer (I once had the same question). You use the "$this->functionname()" to call a function from a function that is defined WITHIN the same model / class.


<?php

MODEL

FUNCTION A {
$this->FUNCTION C
delete * table b
}

FUNCTION B {
$this->FUNCTION C
delete * table a
}

FUNCTION C {
insert into table a field a, b. parametres -> post xxxxx
}

?>[/quote]

I still don't quite get what he wants, as far as I can read he now wants to use the same function for multiple tasks, if so you could do the following:

Code:
<?php
function foo($param,$type) {
  if($type == 'insert') {
    $this->db->insert($param);
  }
  if($type == 'delete') {
    $this->db->delete($param);
  }

}
?>

#8
[eluser]tnathos[/eluser]
No works it… generate the next error..

Call to undefined method ClientModel::restTrans() in application\models\clientModel.php on line 118

code example

Code:
<?php
class ClientModel extends Model {
    
    function ClientModel()
    {
        parent::Model();
    }

function newTrasac()
    {
    $this->restTrans;
        }

function restTrans (){
        echo "anything...";
        }

?>

#9
[eluser]Yorick Peterse[/eluser]
You forgot the () after $this->restTrans;

It should be :


Code:
<?php
class ClientModel extends Model {
    
    function ClientModel()
    {
        parent::Model();
    }

function newTrasac()
    {
    $this->restTrans();
        }

function restTrans (){
        echo "anything...";
        }

?>

#10
[eluser]tnathos[/eluser]
same error....

Fatal error: Call to undefined method ClientModel::resta() in


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.