将对象从全局范围传递到模型 [英] passing objects from the global scope to a model

查看:107
本文介绍了将对象从全局范围传递到模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's the code of my model in 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 */

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

I want to see the objects in the global scope in the model. I've tried a lot of ways to do this. but there's no way.

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

This are the error when I access to the model that it's not making sense.

Message: Trying to get property of non-object

PD :我不想要替代品。我知道替代品,我可以让它工作在10分钟:)我只是好奇,如果这可以工作与否。

P.D.: I don't want alternatives. I know the alternatives and I can make it work in 10 minutes :) I'm just curious about if this can work or not.

推荐答案

你不能,这是不可能的,即使Chuck Norris可以访问类定义中的全局变量。
你必须将它们传递给构造函数或方法当你需要这些对象。想一想:OOP背后的整个想法是编写代码一次,可以部署在任何地方,如果该代码依赖于一个全局变量(称为$ foo)在全局范围内定义,则代码不能重复使用,除非全局范围有所需的对象,与那些特定的名称...如果这被允许,OOP代码将是地狱调试:目前,如果你得到一个错误说未定义,在第x行一个类文件,你知道你正在尝试访问一个属性。想象一下,不得不通过每一行代码的恐惧,只是为了找出你写的:$ foo而不是全局$foo└

You can't, it's impossible, not even Chuck Norris can access global variables inside a class definition.
You'll have to pass them to the constructor, or to the method when you need these objects. Think about it: the whole idea behind OOP is to write code once, which can be deployed anywhere, if that code depends on a global variable, called $foo to be defined in the global scope, your code cannot be reused, unless the global scope has the desired objects, with those specific names... If this were allowed, OOP code would be hell to debug: currently, if you get an error saying something is not defined, on line x of a class file, you know that you're trying to access a property. Imagine the horror of having to wade through every line of code, just to find out you wrote: $foo instead of global $foo

额外:

只是想到一个简单的方法来分类这个工作:将对象分配给一个超全局变量(如$ _GLOBALS)将给你访问(超全局可以从类中访问)。但这只是可怕的做法,并且非常容易出错:每个类都可以访问这些对象,并可能重新分配或更改数据,所以你不能保证对象仍然存在...

Extra:
Just thought of a hacky way to sort-of get this to work: assigning the objects to a super-global variable (like $_GLOBALS) will give you access (super-globals can be accessed from within a class). But that's just terrible practice and Very error-prone: every class has access to those objects, and might reassign or change the data, so you have no guarantee that the objects still exist...

坦率地说,如果您需要访问全局变量,最好花时间考虑您实际上想要做什么。对于你的,事实上每个人的缘故,你最好还是选择,写一切OO,或什么也没有。混合两者只会导致最后的悲伤。

Frankly, if you need access to global variables, you'd better take the time to think about what you actually want to do. For yours, and indeed everybody's sake, you'd better choose, either write everything OO, or nothing. Mixing the two will only cause you grief in the end.

因为你使用codeigniter,为什么不花时间去了解框架,并按照它的意图使用它。这样,你避免重复发明轮子,毕竟:这是什么框架:他们为你提供一个(相当)发展良好的骨干,做所有重型起重和nitty-gritty的东西,所以你不必。

只需使用框架并查看MVC模式,这就是我要做的...

Since you're using codeigniter, why not take the time to get to know the framework, and use it as it was intended. That way, you avoid reinventing the wheel and, after all: that's what frameworks are for: they offer you a (fairly) well developed backbone that does all the heavy lifting and nitty-gritty stuff so you don't have to.
Just use the framework and look into the MVC pattern, that's what I'd do...

编辑:

你得到的错误是绝对有意义的:访问非对象的属性是期望的,因为你不能使用全局变量。 PHP有这种倾向,沿着排序,试图使它所有的工作,所以它可能为你定义一个局部变量,因为它没有初始化将被分配为NULL,null不是一个对象,因此您无法访问属性。因此:访问非对象的属性。


The error you're getting makes absolute sense: accessing a property of a non object is to be expected, because you can't use global variables. PHP has this tendency to sort of blunder along, and try to make it all work, so it probably defines a local variable for you, which (as it's not initialized will be assigned NULL), null is not an object, and so you can't access properties. Hence: accessing properties of a non-object.

更正

as @fireeyedboy指出我,可以这样做(检查链接到键盘的注释)。我仍然认为这是一个可怕的方式代码,甚至可以称为这个bug,需要被压扁ASAP。

如果我看到一个氰化物丸的代码相当,这是它。写OO代码,只有当某些全局变量存在时才能工作是一种犯罪再次人道!

Correction
as @fireeyedboy pointed out to me, it is possible to do this (check comments for link to codepad). I still maintain that this is a terrible way to code, and would even go as far as to call this a bug, that needs to be squashed ASAP.
If ever I saw a code-equivalent of a cyanide-pill, this is it. Write OO code, that can only work if certain global variables exist is a crime agains humanity!

这篇关于将对象从全局范围传递到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆