Welcome Guest, Not a member yet? Register   Sign In
Custom library extending custom libraries
#11

[eluser]patbert[/eluser]
I have two files in my application/libraries directory: Listing.php and Shared.php

Shared.php:
class Shared { }

Listing.php:
class Listing extends Shared { }

I then load Listing in my controller and get the error:
Fatal error: Class 'Shared' not found in application/libraries/Listing.php on line 3
#12

[eluser]christian.schorn[/eluser]
I use CIs "load_class" function for this, better than maintaining include directives by hand.

The base library would be in application/libraries/base_lib.php, and application/libraries/extended_lib.php would look like this:

Code:
<?php
load_class('base_lib', false);

class Extended_lib extends Base_lib {
//... library code
}
#13

[eluser]patbert[/eluser]
[quote author="christian.schorn" date="1209865702"]I use CIs "load_class" function for this, better than maintaining include directives by hand.

The base library would be in application/libraries/base_lib.php, and application/libraries/extended_lib.php would look like this:

Code:
<?php
load_class('base_lib', false);

class Extended_lib extends Base_lib {
//... library code
}
[/quote]

Woah, that I can do. Where is load_class documented?
#14

[eluser]christian.schorn[/eluser]
system/codeigniter/Common.php Smile

Normally I don't like to use internal functions, they are generally undocumented for a reason, but in this case I think it's ok, since whenever the structure of such a basic function changes, you'll have to restructure your application anyway.
#15

[eluser]patbert[/eluser]
Now I have another issue.
Shared.php
Code:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Shared {

    function __construct() {
        $this->ci =& get_instance();
        $this->ci->config->load('movie');

        $this->titletbl = $this->ci->config->item('titles_table');
        $this->codectbl = $this->ci->config->item('codec_table');
        $this->cntnrtbl = $this->ci->config->item('container_table');
        $this->drctrtbl = $this->ci->config->item('director_table');
        $this->disctbl  = $this->ci->config->item('disc_table');
        $this->dsctptbl = $this->ci->config->item('disc_type_table');
        $this->genretbl = $this->ci->config->item('genre_table');
        $this->qualtbl  = $this->ci->config->item('quality_table');
        $this->cntrytbl = $this->ci->config->item('country_table');
        $this->cstmtbl  = $this->ci->config->item('custom_table');
        $this->cstmrtbl = $this->ci->config->item('custom_relation_table');

        $this->custom_field_prefix = $this->ci->config->item('custom_field_prefix');

        $this->imdb_title_root = $this->ci->config->item('imdb_title_root');
        $this->imdb_name_root  = $this->ci->config->item('imdb_name_root');

        $this->image_dir       = $this->ci->config->item('image_dir');

    }

    function make_site_link($uri, $id, $linktext) {
        $site_link_html = '<a href="' . site_url($uri . $id) . '">' . $linktext . '</a>';
        return $site_link_html;
    }
}

Then Title.php
Code:
&lt;?php if (! defined('BASEPATH')) exit('No direct script access allowed');

load_class('Shared', false);

class Title extends Shared {

    private function get_custom_field_names() {
        $striplen = strlen($this->custom_field_prefix);

        $field_list = $this->ci->db->list_fields($this->titletbl);

        foreach ($field_list as $field) {
            if (strpos($field, $this->custom_field_prefix) !== false) {
                $fields[] = $field;
            };
        };

        if (! empty($fields)) {
            $this->custom_field_names->fields = (object) $fields;
            $this->custom_field_names->striplen = $striplen;
        };

    }

}

There is a bunch of other stuff I cut out. The problem is that in the get_custom_field_names() function when it tries to read $this->custom_field_prefix I get an error that it is an undefined property.
#16

[eluser]Spockz[/eluser]
That's correct.

That variable/property isn't declared in your Shared class.

Are you sure the __construct() is called if you instantiate Title?
#17

[eluser]patbert[/eluser]
I would say it must be. Look at this.

Code:
&lt;?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Listing extends Shared {

    public $type       = 'all';
    public $order      = 'title';
    public $direction  = 'asc';
    public $pagelength = 50;
    public $offset     = 1;

    public $total_rows;
    
    public $title_search;
    public $director_search;

    public $query;
    public $page_links;

    public $table;

    private $default_array = array('type',
                       'order',
                       'direction',
                       'pagelength',
                       'offset');

    public function get_list() {
        $wantedrows = array (
            'id_field',
            'title_name',
            'title_imdb_url',

            "$this->titletbl.disc_id",
            "$this->disctbl.disc_type_id",
            'disc_type_name',

            "$this->titletbl.director_id",
            'director_url',
            'director_name',

            "$this->titletbl.genre_id",
            'genre_name',

            "$this->titletbl.country_id",
            'country_code',
            'country_name',

            'title_imdb_rating',
            'title_imdb_year'
        );

        $orderby_array = array ('title' => 'title_name',
                   'director' => 'director_name',
                   'year' => 'title_imdb_year',
                   'rating' => 'title_imdb_rating',
                   'disc' => 'disc_id');
        
        $orderby_column = 'title_name';

        foreach ($orderby_array as $orderby_key => $orderby_name) {
            if ($this->order == $orderby_key) {
                $order_column = $orderby_name;
            };
        };

        $loop = 0;

        do {
            $this->ci->db->select($wantedrows, FALSE);
            $this->ci->db->from($this->titletbl);
            $this->ci->db->join($this->disctbl, "$this->titletbl.disc_id = $this->disctbl.disc_id", 'left');
            $this->ci->db->join($this->dsctptbl, "$this->disctbl.disc_type_id = $this->dsctptbl.disc_type_id", 'left');
            $this->ci->db->join($this->drctrtbl, "$this->titletbl.director_id = $this->drctrtbl.director_id", 'left');
            $this->ci->db->join($this->genretbl, "$this->titletbl.genre_id = $this->genretbl.genre_id", 'left');
            $this->ci->db->join($this->cntrytbl, "$this->titletbl.country_id = $this->cntrytbl.country_id", 'left');

            if (! empty($this->title_search)) {
                $this->ci->db->or_like('title_name', $this->title_search);
                $this->ci->db->or_like('title_name_alt', $this->title_search);
            } elseif (! empty($this->director_search)) {
                $this->ci->db->or_like('director_name', $this->director_search);
            };

            $this->ci->db->orderby($order_column, $this->direction);
            $this->ci->db->orderby('title_name');

            if ($loop == 1) {
                $this->ci->db->limit($this->pagelength, $this->offset);
            };

            $this->query = $this->ci->db->get();

            if ($this->query->num_rows() < 1) {
                echo 'No results, aborting';
                exit();
            };

            if ($loop == 0) {
                $totalrows = $this->query->num_rows();
            };
            $loop++;
        } while ($loop != 2);

        $this->total_rows = $totalrows;

        $this->make_page_links();
    }
}

Again, there is more to that class, but it runs fine and it is reading lots of things from Shared.php that were declared in Shared's __construct() function.
#18

[eluser]christian.schorn[/eluser]
Did you define a constructor in Title? If you did, did you call parent::__construct?
#19

[eluser]patbert[/eluser]
Oh shi. . .I did do that. Thank you!

Code:
function __construct() {
        parent::__construct();
        $this->get_custom_field_names();

    }

So is this correct? I am getting a very generic CI error now that says:

"To fetch fields requires the name of the table as a parameter."

I think that is the line $field_list = $this->ci->db->list_fields($this->titletbl); in the get_custom_field_names() function. It is almost like $this->titletbl is not set to anything?
#20

[eluser]christian.schorn[/eluser]
What does it say, if you do
Code:
var_dump($this->titletbl);
before that line?




Theme © iAndrew 2016 - Forum software by © MyBB