How to test if dimension element exists

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • How to test if dimension element exists

    Hi, I am using the PHP API and want to add an element but have to deal with the case, that it is already there. I do not want to fire exceptions when doing so.

    Is there a solution that I cannot see or is palo_dimension_list_elements () the only way? This would seem clumsy and does not perform well for larger dimensions ...

    Any hints?

    Thanks,
    Mario
  • RE: How to test if dimension element exists

    Hi Mario,
    I am not quite sure about what kind of error it returns when element does not exist but how about

    palo_eindex (line 459)
    Returns the position in the dimension of the specified elements

    return: of the element or error on failure
    Position palo_eindex (string $connection_and_db, string $dimension_name, string $element_name)
    string $connection_and_db: "connection_name/database_name"
    string $dimension_name: Name of the dimension which contains the element
    string $element_name: Name of the element


    Greetings from Cologne
    Holger
  • RE: How to test if dimension element exists

    Hi Holger,

    tried this already and it also generates an error which would either go to the error_log file or into the web page :(

    I think that without a testing operator the interface is not really complete ... maybe I put it into the bug system.

    I have created some quite smart classes now that encapsulate the job of adding these elements if they are new, so the main programme stays clean and readable.

    Thanks,
    Mario
  • RE: How to test if dimension element exists

    Hello Irbis,

    Actually you are right: eadd should return false or true. Please put it into
    the bug tracker. A circumnavigation would be to set the error_reporting()
    on 0 or E_NOTICE and get false or int() with palo_eindex() and work with
    that - or another solution:

    $elements = palo_dimension_list_elements($connection, 'Demo', 'Products');

    $newelement = "Desktop XXXL";

    foreach($elements as $value){
    $elementsnew[] = $value["name"];
    }

    if(in_array($newelement, $elementsnew)){
    echo "Yes, it's already here.";
    }
    else{
    echo "No, it's not..please add it!";

    }

    Regards,
    Stephanie
  • RE: How to test if dimension element exists

    Well quite so I did it:

    [PHP]
    class dimension {
    public $Liste=array();
    public $name="undefined";
    public $all="All";
    function __construct($myname,$myall) {
    global $connection,$database;
    $this->name=$myname;
    $this->all=$myall;
    $this->Liste=palo_dimension_list_elements($connection,$database,$this->name);
    // this is necessary because the first element returns an index of 0 which is wrongly interpreted as false
    array_unshift($this->Liste,'dummy');
    }

    function introduce($element,$parent=FALSE) {
    global $connection,$database;
    if (empty($element)) { $element="undefined"; }
    if (!array_search($element,$this->Liste)) {
    // the following is just for test purposes
    echo "adding |$element| in ".$this->name." <br>";
    if ($parent) {
    $res=palo_eadd($connection, $database, $this->name, 'N', $element, $parent, 1, FALSE);
    } else {
    $res=palo_eadd($connection, $database, $this->name, 'N', $element, $this->all, 1, FALSE);
    }
    array_push($this->Liste,$element);
    }
    return $element;
    }
    }
    [/PHP]

    That is quite save and moderately fast. I still think it should be handled by a proper API function though.

    Ciao,
    Mario

    The post was edited 1 time, last by Irbis ().

  • RE: How to test if dimension element exists

    Hello Stephanie,

    Yes, I am of course aware of that option to fiddle with error_reporting() but I never really encouraged others to do so so I will not start it. In an environment which is complete and functioning the programming style should head towards a setup where all options are turned on and still the error_log stays clean. "Let the interpreter take care of undefined variables" and similiar is never a good way to program.

    So I rather went for a class that circumvents the problem - maybe until there is a more sophisticated support functionality introduced into the API.

    Thanks anyway,
    Mario