Foros del Web » Programando para Internet » PHP » Frameworks y PHP orientado a objetos »

Cake php error Call to a member function

Estas en el tema de Cake php error Call to a member function en el foro de Frameworks y PHP orientado a objetos en Foros del Web. Hola , tengo un problemilla que no consigo solucionar: He creado un método en el modelo Nivele (tabla niveles) Código: class Nivele extends AppModel { ...
  #1 (permalink)  
Antiguo 22/01/2010, 05:37
Avatar de Dundee  
Fecha de Ingreso: junio-2002
Ubicación: El Médano
Mensajes: 1.310
Antigüedad: 21 años, 9 meses
Puntos: 8
Cake php error Call to a member function

Hola , tengo un problemilla que no consigo solucionar:
He creado un método en el modelo Nivele (tabla niveles)
Código:
class Nivele extends AppModel {

    var $name = 'Nivele';

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'Juego' => array(
            'className' => 'Juego',
            'foreignKey' => 'juego_id' ) );

    var $hasMany = array(
        'partidas' => array(
            'className' => 'partidas',
            'foreignKey' => 'nivele_id',
            'dependent' => false       ) );
function nivelMayor($juego_id) {
    if (empty($juego_id))
    return false;
      $nivel=$this->Nivele->field('Numero_Nivel',array('juego_id' =>$juego_id)); (línea que tira el error)
                                                      
    if (!$nivel)
    return false;
    else {
    $this->Session->setFlash(__('Nivele número '. $nivel, true));
        $this->redirect(array('action'=>'index'));
    }}
function cualesMayor($juego_id,$numero_nivel) {
    $mayor=NivelMayor($juego_id);
    if ($numero_nivel >$mayor ) {
    $this->Nivele->User->activar($id=null,4) ; //estado para juego finalizado
    return true;
    }
    return $numero_nivel;
    }
}
Este modelo Nivele está relacionado con otro modelo llamado Juego (Juego has many Niveles y Niveles BelongsTo Juego )
Modelo Juego
Código:
class Juego extends AppModel {

	var $name = 'Juego';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
		'Nivele' => array(
			'className' => 'Nivele',
			'foreignKey' => 'juego_id',
			'dependent' => false
			
		));

}
En juegos_controller tengo este sencillo método que llama al método anteriormente creado en Nivele (aquí está el problema):
Código:
function ajugar($user_id,$juego_id,$numero_nivel) {
   
    $mayor=$this->Juego->Nivele->nivelMayor($juego_id);
    $this->redirect(array('action'=>'index'));
   
    }
Como ambos modelos (nivele y juego) están relacionados no he creído conveniente usar var $uses=array('Nivele','Juego') (aún así he probado y tampoco funciona);

El error que me tira es el siguiente:


Undefined property: Nivele::$Nivele [APP\models\nivele.php, line 36]
Fatal error: Call to a member function field() on a non-object in C:\xampp\htdocs\tests\app\models\nivele.php on line 36

Agradecería si alguien me puede echar un cable porque llevo horas rompiéndome la sesera y no se donde esta el fallo.
Mil gracias a tod@s de antemano.
__________________
Videotutoriales de Drupal
  #2 (permalink)  
Antiguo 22/01/2010, 06:50
Avatar de jcxnet  
Fecha de Ingreso: octubre-2005
Ubicación: Perú
Mensajes: 784
Antigüedad: 18 años, 6 meses
Puntos: 56
Respuesta: Cake php error Call to a member function

Hola,
al crear la función dentro del modelo 'Nivele' ya no deberías hacer esta llamada: $nivel=$this->Nivele->field('Numero_Nivel',array('juego_id' =>$juego_id));
ya que estás dentro del objeto Nivele, prueba cambiarla a:
$nivel=$this->field('Nivele.Numero_Nivel',array('Nivele.juego_i d' =>$juego_id));

__________________
►I'm a devil on the run ♂
Jcxnet.com
*Keep It Simple **
  #3 (permalink)  
Antiguo 22/01/2010, 08:27
Avatar de Dundee  
Fecha de Ingreso: junio-2002
Ubicación: El Médano
Mensajes: 1.310
Antigüedad: 21 años, 9 meses
Puntos: 8
Respuesta: Cake php error Call to a member function

Cita:
Iniciado por jcxnet Ver Mensaje
Hola,
al crear la función dentro del modelo 'Nivele' ya no deberías hacer esta llamada: $nivel=$this->Nivele->field('Numero_Nivel',array('juego_id' =>$juego_id));
ya que estás dentro del objeto Nivele, prueba cambiarla a:
$nivel=$this->field('Nivele.Numero_Nivel',array('Nivele.juego_i d' =>$juego_id));

Mil gracias compañero, era eso ¡¡no había caído!!
Un saludo
__________________
Videotutoriales de Drupal

Etiquetas: cake, call, function, php
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:56.