Skip to:
Content
Pages
Categories
Search
Top
Bottom

Question about classes and saving

  • Avatar of Grosbouff
    grosbouff
    Participant

    @grosbouff

    Hello; I have a class Classifieds which records …classifieds.

    There are many parameters, with also “type” and “category”. I would like to save those ones in another table that the classifields table.

    Why ? Because i guess that maybe some users (or maybe me later) will want to assign several types / categories to a classified.

    So I would like to add it like a meta (you can have several metas “category” for a same classified id, etc.) in my Meta table.

    My question is :

    How must I do this ?

    in my php :

    $classified_obj->type = $_POST['type'];
    $classified_obj->category = $_POST['category'];
    if ( !$classified_obj->save() )return false;

    in my Classified class :

    function save() {
    if ( $this->id ) {
    $sql = $wpdb->prepare(
    "UPDATE {$bp->classifieds->table} SET
    ...
    } else {
    $sql = $wpdb->prepare(
    "INSERT INTO {$bp->classifieds->table} (
    ...
    }

    if ( false === $wpdb->query($sql) )
    return false;

    if ( !$this->id ) {
    $this->id = $wpdb->insert_id;
    }

    Where must I save my metas ?

    My Class BP_Classifieds_Metas takes only two parameters :

    function bp_classifieds_metas($classified_id=null,$meta_key=null) {

    So it won’t populate. (I guess this is not important here).

    Do I have to do something like this inside the ‘save’ function of my Classified class ?

    $meta=new BP_Classifieds_Metas;
    $meta->classified_id=$this->id;
    $meta->key='type';
    $meta->value=$this->category;
    $meta->save();

    ?

    And if yes, how must I handle the errors now, for example if my Metas query fails but that my Classified query succeed ?

    Thanks a lot.

You must be logged in to reply to this topic.