Welcome Guest, Not a member yet? Register   Sign In
Entity get value
#1

(This post was last modified: 07-04-2021, 06:26 AM by pippuccio76.)

hi sorry for english i have a model(Tratte_libere_mezziModel) with a simple sql query :

Code:
$db = \Config\Database::connect();

  $sql = '
                    SELECT  mezzi_tratte.*,mezzi.id as id_tabella_mezzi,mezzi.immagine,mezzi.nome,mezzi.descrizione as mezzi_descrizione,mezzi.passeggeri,mezzi.bagagli
                    FROM tratte_libere as tratte
                    JOIN tratte_libere_mezzi as mezzi_tratte
                    ON tratte.id = mezzi_tratte.id_tratte_libere
                    JOIN mezzi
                    ON mezzi_tratte.id_mezzi = mezzi.id
                    WHERE TRUNCATE ( 6363 * sqrt( POW( RADIANS("'.$latitudine_partenza.'") - RADIANS(tratte.latitudine_partenza) , 2 ) + POW( RADIANS("'.$longitudine_partenza.'") - RADIANS(tratte.longitudine_partenza) , 2 ) ) , 3 ) < tratte.raggio_ricerca_km
                    AND TRUNCATE ( 6363 * sqrt( POW( RADIANS("'.$latitudine_arrivo.'") - RADIANS(tratte.latitudine_arrivo) , 2 ) + POW( RADIANS("'.$longitudine_arrivo.'") - RADIANS(tratte.longitudine_arrivo) , 2 ) ) , 3 ) < tratte.raggio_ricerca_km
                    AND mezzi.passeggeri >= '.$passeggeri.'
                    AND mezzi.bagagli >= '.$bagagli.'
                    ORDER BY mezzi_tratte.costo
                    ';
           

            $res=$db->query( $sql )->getResult();
            $data['lista'] = $res;

in entity (Tratte_libere_mezzi_Entity) i have a function prezzo_sconti_maggiorazioni($id_tratte_libere_mezzi) 

in view in foreach loop :

Code:
<?php foreach ($lista as $v): ?>



if i try to do $v->prezzo_sconti_maggiorazioni($v->id)

Call to undefined method stdClass::prezzo_sconti_maggiorazioni()

wat's wrong ?  
Reply
#2

The returned object is not your entity. If you use the model's function (find, findAll, etc) it will return the data in entity objects, but in your example you're calling the query builder, not the model's function that adds the entity automatically. If you look at the model's code, you can see it pass what you set as the return type to getResult(). So I think if you pass your entity type it would return what you expect. Something like this:
PHP Code:
$res=$db->query$sql )->getResult('App\Entities\Tratte_libere_mezzi_Entity'); 

...or even better, pass the return type you already defined in your model:
PHP Code:
$res=$db->query$sql )->getResult($this->returnType); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

Entity's use to change or alter or some operation before insert data in table db

Oky
Enlightenment  Is  Freedom
Reply




Theme © iAndrew 2016 - Forum software by © MyBB