0

这是我在codeigniter中的模型代码。

<?php

$catalogo = (object) array (
    "name" => "catalogo",
    "url" => "url_catalogo"
);
$categorias = (object)array (
    "name" => "categorias",
    "titulo" => "varchar_titulo",
);

$novedades = (object)array (
    "name"      => "novedades",
    "titulo"    => "varchar_titulo",
    "fecha"     => "fecha_publicacion",
    "descripcion"   => "text_descripcion",
    "categoria" => "fk_categoria"
);

$img_nov =(object) array (
    "name"      => "imagenes_novedades",
    "url"       => "url_img",
    "novedad"   => "fk_novedad"
);  
$marcas = (object) array(
    "name"      => "marcas",
    "titulo"    => "varchar_titulo",
    "url"       => "url_imagen"
);
$img_slide  = (object) array (
    "name"  => "slide_destacados",
    "url"   => "url_img"
);

$users = (object) array (
    "db"    => "users",
    "user"  => "username",
    "pass"  => "password"
);
class Model extends CI_Model 
{

    function __construct()
    {
        parent::__construct();
    }

    function check_user ( $username, $password )
    {
        global $users;
        if ( $username == "" || $password == "" )
            return FALSE;

        $hashed_password = md5( $password ); 

        $this->db->where($users->user, $username );
        $this->db->where($users->pass, $hashed_password );

        $query = $this->db->get($users->name);

        if ( $query->num_rows() > 0 )
            return TRUE;
        return FALSE;
    }

    /*
     * Slide de destacados de la pagina principal.
     * no creo que necesitemos mas que esto
     */

    function imagenes_slide ( $id = -1 )
    {
        global $img_slide;
        if ( $id == -1 )
            $this->db->where( 'id', $id);
        $query = $this->db->get( $img_slide->name);

        return $query->result_array();
    }

    function borrar_imagen_slide( $id )
    {
        global $img_slide;
        $image_to_delete = $this->imagenes_slide( $id );

        $this->db->where('id', $id);
        $this->db->delete($img_slide->name);

        return $image_to_delete[0][$img_slide->url];
    }

    function agregar_imagen_slide( $url )
    {
        global $img_slide;
        $this->db->insert( $img_slide->name, array( $img_slide->url => $url ));
    }

    function cambiar_imagen_slide ( $id, $url )
    {
        global $img_slide;
        $this->db->where( 'id', $id );
        $this->db->update( $img_slide->name, array( $img_slide->url, $url ) );
    }

    /*
     * Url del catalogo.
     */

    function url_catalogo( $id = -1 )
    {
        global $catalogo;
        if ( $id !== -1 )
            $this->db->where( 'id', $id );

        $query = $this->db->get( $catalogo->name );
        return $query->result_array();
    }

    function add_catalogo ( $url )
    {
        global $catalogo;
        $this->db->insert( $catalogo->name, array( $catalogo->url, $url) );
    }

    function remove_catalogo ( $id )
    {
        global $catalogo;
        $url_catalogo = $this->url_catalogo( $id );

        $this->db->where( 'id', $id );
        $this->db->delete( $catalogo->name );

        # Es solo que queremos el primero, ya que buscamos por id
        # y este es unico.
        return $url_catalogo[0][$catalogo->url];
    }

    function update_catalogo ( $id, $url )
    {
        global $catalogo;
        $this->db->where( 'id', $id );
        $this->db->update( $catalogo->name, array( $catalogo->url, $url ) );
    }

    /*
     * Marcas.
     */

    function get_marcas ( $id = -1)
    {
        global $marcas;
        if( $id !== -1 )
            $this->db->where( 'id', $id );
        $query = $this->db->get( $marcas->name );
        return $query->result_array();
    }   

    function add_marca ( $titulo, $url )
    {
        global $marcas;
        $n = array( $marcas->titulo => $titulo, $marcas->url => $url );
        $this->db->insert( $marcas->name, $n );
    }

    function remove_marca ( $id )
    {
        global $marcas;
        # Get the url to delete the image
        $url_to_delete = $this->get_marcas( $id );

        $this->db->where( 'id', $id );
        $this->db->delete( $marcas->name );

        # Es solo que queremos el primero, ya que buscamos por id
        # y este es unico.
        return $url_to_delete[0][$marcas->url];
    }

    function update_marca ( $id, $titulo = FALSE, $url = FALSE )
    {
        global $marcas;
        $to_update = array ();

        if ( $titulo != FALSE )
            $to_update[$marcas->titulo] = $titulo;
        if ( $url != FALSE )
            $to_update[$marcas->url] = $url;

        if ( $to_update !== array() )
        {
            $this->db->where( 'id', $id );
            $this->db->update($marcas->name, $to_update );
        }
    }

    /*
     * Categorias!
     */
    function get_categorias ( $id = -1 )
    {
        global $categorias;
        if ( $id != -1 )
            $this->db->where('id', $id);
        $query = $this->db->get( $categorias->name );
        return $query->result_array();
    }

    function remove_categoria ( $id )
    {
        global $categorias;
        # Conseguimos todos los items de las categorias.
        $novedades = $this->get_novedades( $id );
        foreach( $novedades as $novedad )
        {
            $this->delete_novedad ( $novedad["id"] );
        }

        $this->db->where( 'id', $id );
        $this->db->delete( $categorias->name );
    }

    function add_categoria ( $titulo )
    {
        global $categorias;
        $data = array( $categorias->titulo, $titulo );
        $this->db->insert ($categorias->name, $data);
    }

    function update_categoria ( $id, $titulo )
    {
        global $categorias;
        $this->db->where( 'id', $id);
        $this->db->update( $categorias->name, array( $categorias->titulo, $titulo ) );
    }

    /*
     * Novedades ! (Esto va a ser largo)
     */

    function get_novedades ( $id_categoria, $id_novedad = FALSE, $offset = FALSE )
    {
        global $novedades;
        $this->db->where( $novedades->categoria, $id_categoria );
        if ( $id_novedad !== FALSE )
            $this->db->where ( 'id', $id_novedad );
        if ( $offset !== FALSE )
            $this->db->limit( 10, $offset );
        $query = $this->db->get( $novedades->name );

        return $query->result_array();
    }

    function add_novedad ( $titulo, $descripcion, $id_categoria )
    {
        global $novedades;
        # Hay que crear la fecha actual.
        $date = new Date();

        $to_add = array (
            $novedades->titulo  => $titulo,
            $novedades->fecha   => $date,
            $novedades->descripcion => $descripcion,
            $novedades->categoria   => $id_categoria
        );
        $this->db->insert( $novedades->name, $to_add );
    }

    function delete_novedad ( $id )
    {
        global $novedades;

        # Y ahora sacamos de la base de datos.
        $this->db->where( 'id', $id );
        $this->db->delete( $novedades->name );

        # Retornamos todas las urls de las imagenes.
    }

    function update_novedad ( $id, $titulo = FALSE, $descripcion = FALSE )
    {
        global $novedades;
        $to_update = array();
        $this->db->where ( 'id' , $id );
        if ( $titulo != FALSE )
            $to_update[$novedades->titulo] = $titulo;
        if ( $descripcion != FALSE )
            $to_update[$novedades->descripcion] = $descripcion;

        if ( $to_update === array() )
            return FALSE;
        $this->db->update( $novedades->name, $to_update );
    }

    /*
     * Aca van las imagenes de las novedades.
     */

    function get_images_novedad ( $id_novedad, $amount = 0 )
    {
        global $img_nov;
        if ( $amount != 0 )
            $this->db->limit( $amount );
        $this->db->where( $img_nov->novedad, $id_novedad );
        $query = $this->db->get( $img_nov->name );

        return $query->result_array();
    }

    function delete_imagen_novedad ( $id )
    {

        global $img_nov;
        # Primero que nada, eliminamos todas las imagenes. :)
        $images = $this->get_imagenes_novedad ( $id );
        $to_delete_permantenlty = array();
        foreach ( $images as $image )
        {
            $to_delete_permantenlty[] = $image[$img_nov->url];
        }

        $this->db->where( $img_nov->novedad, $id );
        $this->db->delete( $img_nov->name );
        return $to_delete_permantenlty;
    }

}
/* End of Model class */

我想在模型中查看全局范围内的对象。我已经尝试了很多方法来做到这一点。但没有办法。

当我访问没有意义的模型时,这是错误的。

Message: Trying to get property of non-object

PD:我不想要替代品。我知道替代方案,我可以在 10 分钟内使其工作:) 我只是好奇这是否可以工作。

4

1 回答 1

3

你不能,这是不可能的,甚至 Chuck Norris 也不能访问类定义中的全局变量。
当您需要这些对象时,您必须将它们传递给构造函数或方法。想一想:OOP 背后的整个想法是编写一次代码,它可以部署在任何地方,如果该代码依赖于一个名为 $foo 的全局变量,要在全局范围内定义,那么您的代码就不能被重用,除非全局范围具有所需的对象,具有这些特定的名称...如果允许,OOP 代码将很难调试:目前,如果您收到错误说未定义,在第 x 行class 文件,你知道你正在尝试访问一个属性。想象一下,不得不遍历每一行代码,才发现你写的是:$foo 而不是全局 $foo

额外:
只是想到了一种让它工作的hacky方法:将对象分配给超级全局变量(如$_GLOBALS)将为您提供访问权限(可以从类中访问超级全局变量)。但这只是一种糟糕的做法并且非常容易出错:每个类都可以访问这些对象,并且可能重新分配或更改数据,因此您无法保证这些对象仍然存在......

坦率地说,如果您需要访问全局变量,您最好花时间考虑一下您真正想要做什么。为了你,也为了每个人,你最好选择,要么写所有的面向对象,要么什么都不写。将两者混合在一起最终只会让您感到悲伤。

既然您使用的是 codeigniter,为什么不花时间了解一下该框架,并按预期使用它。这样,您就可以避免重新发明轮子,毕竟:这就是框架的用途:它们为您提供了一个(相当)发达的主干,可以完成所有繁重和琐碎的工作,因此您不必这样做。
只需使用框架并查看 MVC 模式,这就是我要做的......

编辑:
你得到的错误是绝对有意义的:访问非对象的属性是可以预期的,因为你不能使用全局变量。PHP 有这种错误的倾向,并试图让它全部工作,所以它可能为你定义了一个局部变量,它(因为它没有被初始化将被分配 NULL),null 不是一个对象,所以你可以'不访问属性。因此:访问非对象的属性。


正如@fireeyedboy 向我指出的那样更正,可以这样做(检查注释以获取指向键盘的链接)。我仍然认为这是一种糟糕的编码方式,甚至会称之为错误,需要尽快解决。
如果我曾经看到过与氰化物药丸等效的代码,就是这样。编写 OO 代码,只有在某些全局变量存在的情况下才能工作,这是对人类的犯罪!

于 2012-12-28T19:59:40.120 回答