Problem with query

I use this code

        error_log('Checking for duplicate offer: listing=' . $listing_id . ', user=' . $current_user_id);
        // Prevent duplicate offers if desired (optional)

        $listing_count = \HivePress\Models\Best_Offer::query()->filter(
            [
                'listing' => $listing_id,
                'user'    => $current_user_id,        // Will map to user_id
            ]
        )->get_count();

        error_log( print_r( $listing_count , true ) );
                
        $existing_offer = \HivePress\Models\Best_Offer::query()
            ->filter([
                'listing' => $listing_id,             // Will map to comment_post_ID
                'user'    => $current_user_id,        // Will map to user_id
//                'status'  => 'pending',               // Will map to commentmeta if not a native column
            ])
            ->get();
        
         error_log( print_r( $existing_offer->count() , true ) );
         //error_log( print_r( $existing_offer, true ) );

To check for duplicate offer in the database. To confirm the values are there and valid in the query i have logged them to the error log:

There is one record in the wp_commments table with the type==best_offer:

The comment_post_id (0) in the table does not match the listing of the filter (256), also the user does not match but it is outside the screenshot.

Considering i used 2 diffrent approaches with the same results (the return of the only 1 record in the table), the filter seems to not do its job. What am i doing wrong?

Edit ot add the model:

				// Define the model fields.
				'fields' => [
					'price' => [
						'type'     => 'number',
						'label'    => 'Offered Price',
						'required' => true,
					],
					'message' => [
						'type'  => 'text',
						'label' => 'Message',
					],
					'sent_date'            => [
						'type'   => 'date',
						'format' => 'Y-m-d H:i:s',
						'time'   => true,
						'_alias' => 'comment_date',
					],
					'status' => [
						'type'    => 'text',
						'label'   => 'Status',
						'default' => 'pending', // pending, accepted, declined, countered
					],
					'listing' => [
						'_alias'    => 'comment_post_ID',
						'type'     => 'link',
						'model'    => 'listing',
						'label'    => 'Listing',
						'required' => true,
					],
					'user' => [
						'_alias'   => 'user_id',						
						'type'     => 'link',
						'model'    => 'user',
						'label'    => 'User',
						'required' => true,
					],

I also have an issue saving the model to the database

        // Create offer
        $offer = new Models\Best_Offer();
        $offer->fill( [
            'listing'   => $listing_id,
            'user'      => $current_user_id,
            'price'     => $price,
            'message'   => $message,
            'status'    => 'pending',
            'sent_date' => current_time( 'mysql' ),
        ] );
        error_log( print_r( $offer, true ) );        
        $offer->save();

The model is correctly filled with values according to the entry logged in the error log.

However an entry in the database apears with empy values except for the comment_type which is correctly set.

Perhaps both query issues have the same root cause. Please point me in the right direction to solve this.

Well, my assuption was correct. Both problems have same root cause: Incorrect use or absense of defining _external in the model for appropriate fields.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.